Check if String contains Java String

I am writing a program in which the user enters String in the following format:

"What is the square of 10?" 
  • I need to check if there is a number in the string
  • and then extract only the number.
  • If I use .contains("\\d+") or .contains("[0-9]+") , the program cannot find the number in String, regardless of what the input represents, but .matches("\\d+") will only work if there are only numbers.

What can I use as a search and retrieval solution?

+56
java string regex
03 Sep '13 at 11:16
source share
14 answers

The solution I went with looks like this:

  Pattern numberPat = Pattern.compile("\\d+"); Matcher matcher1 = numberPat.matcher(line); Pattern stringPat = Pattern.compile("What is the square of", Pattern.CASE_INSENSITIVE); Matcher matcher2 = stringPat.matcher(line); if (matcher1.find() && matcher2.find()) { int number = Integer.parseInt(matcher1.group()); pw.println(number + " squared = " + (number * number)); } 

I am sure that this is not an ideal solution, but it met my needs. Thanks everyone for the help. :)

+1
03 Sep '13 at 11:56 on
source share

try it

 str.matches(".*\\d+.*"); 
+131
Sep 03 '13 at 11:19
source share

If you want to extract the first number from the input string, you can do -

 public static String extractNumber(final String str) { if(str == null || str.isEmpty()) return ""; StringBuilder sb = new StringBuilder(); boolean found = false; for(char c : str.toCharArray()){ if(Character.isDigit(c)){ sb.append(c); found = true; } else if(found){ // If we already found a digit before and this char is not a digit, stop looping break; } } return sb.toString(); } 

Examples:

To enter "123abc", the method above will return 123.

For "abc1000def", 1000.

For "555abc45", 555.

For "abc", an empty string will be returned.

+20
03 Sep '13 at 11:38 on
source share

s=s.replaceAll("[*a-zA-Z]", "") replaces all alphabets

s=s.replaceAll("[*0-9]", "") replaces all numbers

if you do over two replacements, you will get the whole special string

If you want to extract only integers from String s=s.replaceAll("[^0-9]", "")

If you want to extract only alphabets from String s=s.replaceAll("[^a-zA-Z]", "")

Happy coding :)

+7
May 24 '14 at 11:30 a.m.
source share
 Pattern p = Pattern.compile("(([AZ].*[0-9])"); Matcher m = p.matcher("TEST 123"); boolean b = m.find(); System.out.println(b); 
+6
Sep 03 '13 at 11:20
source share

The code below is enough for "Check if a string contains strings in Java"

 Pattern p = Pattern.compile("([0-9])"); Matcher m = p.matcher("Here is ur string"); if(m.find()){ System.out.println("Hello "+m.find()); } 
+6
Mar 26 '14 at 12:14
source share

I think this is faster than regular expression.

 public final boolean containsDigit(String s) { boolean containsDigit = false; if (s != null && !s.isEmpty()) { for (char c : s.toCharArray()) { if (containsDigit = Character.isDigit(c)) { break; } } } return containsDigit; } 
+5
Sep 03 '13 at 11:31 on
source share

Try the following pattern:

 .matches("[a-zA-Z ]*\\d+.*") 
+4
Sep 03 '13 at 11:17
source share

I could not find the correct template. Please follow the guide below for a small and sweet solution.

 String regex = "(.)*(\\d)(.)*"; Pattern pattern = Pattern.compile(regex); String msg = "What is the square of 10?"; boolean containsNumber = pattern.matcher(msg).matches(); 
+3
Sep 25 '17 at 15:37
source share
 public String hasNums(String str) { char[] nums = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char[] toChar = new char[str.length()]; for (int i = 0; i < str.length(); i++) { toChar[i] = str.charAt(i); for (int j = 0; j < nums.length; j++) { if (toChar[i] == nums[j]) { return str; } } } return "None"; } 
+2
Jun 14 '16 at 19:57
source share

You can try this

 String text = "ddd123.0114cc"; String numOnly = text.replaceAll("\\p{Alpha}",""); try { double numVal = Double.valueOf(numOnly); System.out.println(text +" contains numbers"); } catch (NumberFormatException e){ System.out.println(text+" not contains numbers"); } 
+1
Sep 03 '13 at 11:22
source share

Since you want to not only look for a number, but also extract it, you should write a small function that does this for you. Follow the letters until you see the number. Ah, just found the code you need in stackoverflow: find an integer in a string . Look at the accepted answer.

+1
Sep 03 '13 at 11:29
source share

.matches(".*\\d+.*") works only for numbers, but not for other characters, such as // or * , etc.

+1
May 15 '17 at 17:51
source share

ASCII is at the beginning of UNICODE, so you can do something like this:

(x> = 97 & x <= 122) || (x> = 65 & x <= 90)

// 97 == 'a' and 65 = 'A', I'm sure you can find out other values ​​...

0
Aug 7 '17 at 2:39 on
source share



All Articles