Java, how to check if a string contains a digit? [duplicate] - java

This question already has answers here:
Check and extract a number from a String in Java
(16 answers)
Closed 5 years ago.
how do I check if a string contains a digit?
public static void main(String[] args)
{
s1 = "Hello32"; //should be true`enter code here`
s2 = "He2llo"; //should be true
s3 = "Hello"; //should be false
}

With a regex you could search at least a digit among any (zero or more) characters:
boolean hasDigit = s1.matches(".*\\d+.*");

Check this it might help you
String regex = "\\d+";
System.out.println("abc45hdg".matches(regex));

In java
public boolean containsNumber(String string)
{
return string.matches(".*\\d+.*");
}

Related

String to String array conversion with global variables [duplicate]

This question already has answers here:
How to Replace dot (.) in a string in Java
(4 answers)
Closed 2 years ago.
I have following problem with java.This method in Class should return String as is.
private String getAsString(Resource res) {
return "We wish you good luck in this exam!\nWe hope you are well pre-\npared.";
}
Then in Constructor this String shlud be converted into array of words
private int index;
private String string_arr[];
public TextFileIterator(Resource res) {
this.index=0;
if(res==null){
throw new NullPointerException();
}
String text=this.getAsString(res);
//text=text.replaceAll("-\n(?=[a-z])", "");
text=text.replaceAll("\\n", "");
text=text.replaceAll("!", " ");
text=text.replaceAll("-", "");
text=text.replaceAll(".", "");
this.string_arr=text.split(" ");
}
Problem is that at the end I get array which is null... what is the problem. I attach the debugger screenshots.
Please could explain me why does it happen?
The culprit is line no 17-
text=text.replaceAll(".", "");
The above line is replacing all of the content with "", because in regex world "." means any character.
Try this instead-
text=text.replaceAll("\\.", "");

How do I correctly get the value of 'ⅻ' (and other similar non-alphabetical characters) and store it in a string? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
Given a character that is not a standard alphabet character, such as 'ⅻ', I'm having problems converting it to a string and retaining it's value. For example If I have:
String myStr = "ⅻⅾ℡ℬ";
Character myChar = myStr.charAt(0);
Then System.out.println('ⅻ' == myChar); returns true, whereas System.out.println("ⅻ" == Character.toString(myChar)); returns false.
Thus my question effectively is how do I correctly get the value of 'ⅻ' and store it in a string?
Both of these conditions return true:
public class Test {
public static void main(String args[]) {
String myStr = "ⅻⅾ℡ℬ";
Character myChar = myStr.charAt(0);
System.out.println('ⅻ' == myChar);
System.out.println("ⅻ".equals(Character.toString(myChar)));
}
}

Remove n number of dot(.) from String in Java [duplicate]

This question already has answers here:
Trim leading or trailing characters from a string?
(6 answers)
Closed 5 years ago.
I have the following string and I want to remove dynamic number of dot(.) at the end of the String.
"abc....."
Dot(.) can be more than one
Try this. It uses a regular expression to replace all dots at the end of your string with empty strings.
yourString.replaceAll("\\.+$", "");
Could do this to remove all .:
String a = "abc.....";
String new = a.replaceAll("[.]", "");
Remove just the trailing .'s:
String new = a.replaceAll("//.+$","");
Edit: Seeing the comment. To remove last n .'s
int dotsToRemove = 5; // whatever value n
String new = a.substring(0, s.length()-dotsToRemove);
how about using this function? seems to work faster than regex
public static String trimPoints(String txt)
{
char[] cs = txt.toCharArray();
int index =0;
for(int x =cs.length-1;x>=0;x--)
{
if(cs[x]=='.')
continue;
else
{
index = x+1;
break;
}
}
return txt.substring(0,index);
}

Having issues with replaceAll() [duplicate]

This question already has answers here:
String replace method is not replacing characters
(5 answers)
Closed 7 years ago.
Maybe I am using replaceAll incorrectly, but I am unable to find why it acts this way. I want to simply remove a $ sign from a string and then output the string.
public class Example{
public static void main(String[] args){
String s = "$50";
s.replaceAll("\\D+", "");
System.out.println(s);
}
}
However, this still outputs the $ symbol with the string. Does anyone know why this is happening?
You need to assign the return value of replaceAll to a variable:
s = s.replaceAll("\\D+", "");
because a String object is immutable.

Java: Check if string only contains 1,0 or a dot [duplicate]

This question already has answers here:
How to use regex in String.contains() method in Java
(6 answers)
Closed 8 years ago.
I'm attempting to check if a string contains only
1
0
.
or a combination of these three.
First I had this code:
public static boolean controleSubnetmask(String mask) {
try {
String[] maskArray = mask.split(".");
int[] subnetmask = new int[4];
//array of string to array of int
for (int i = 0; i < maskArray.length; i++) {
subnetmask[i] = Integer.parseInt(maskArray[i]);
}
return true;
} catch (NumberFormatException e) {
return false;
}
}
but that is rather complicated for what it does, and it doesn't check if only 1 and 0 are entered.
So now I have this, but it seems I misunderstood regular expressions because it doesn't work:
public static void controleSubnetmask(String mask) {
mask = "1100.110...11";
String test = "p";
if (mask.contains("[^10\\.]") == true) {
System.out.println("wrong input");
}
if (test.contains("[^10\\.]") == true) {
System.out.println("wrong input");
}
}
I expected a 'wrong input' message on the test String, which didn't appear. So I believe my regex:
[^01\\.]
is wrong, but I really have no clue how to specify it.
Thanks in advance!
String.contains works with Strings NOT with REGEX. use String.matches
instead. As #lazy points out, you could use Pattern and Matcher classes as well.
test.contains("[^10\\.]") == true
All you are doing here is checking whether test contains the String literals [^10\.]

Categories