I have a String and I want to extract the (only) sequence of digits in the string.
Example: helloThisIsA1234Sample. I want the 1234
It\'s a given that the s
String line = "This order was32354 placed for QT ! OK?";
String regex = "[^\\d]+";
String[] str = line.split(regex);
System.out.println(str[1]);
You can use the following Code to retain Integers from String.
String text="Hello1010";
System.out.println(CharMatcher.digit().retainFrom(text));
Will give you the Following Output
1010
Use a regex such as [^0-9]
to remove all non-digits.
From there, just use Integer.parseInt(String);
You can try this:
String str="java123java456";
String out="";
for(int i=0;i<str.length();i++)
{
int a=str.codePointAt(i);
if(a>=49&&a<=57)
{
out=out+str.charAt(i);
}
}
System.out.println(out);
You can use str = str.replaceAll("\\D+","");
I always like using Guava String utils or similar for these kind of problems:
String theDigits = CharMatcher.inRange('0', '9').retainFrom("abc12 3def"); // 123