Find all numbers in the String
108,321
Solution 1
Use replaceAll:
String str = "qwerty1qwerty2";
str = str.replaceAll("[^0-9]+", " ");
System.out.println(Arrays.asList(str.trim().split(" ")));
Output:
[1, 2]
[EDIT]
If you want to include -
a.e minus, add -?
:
String str = "qwerty-1qwerty-2 455 f0gfg 4";
str = str.replaceAll("[^-?0-9]+", " ");
System.out.println(Arrays.asList(str.trim().split(" ")));
Output:
[-1, -2, 455, 0, 4]
Description
[^-?0-9]+
+
Between one and unlimited times, as many times as possible, giving back as needed-?
One of the characters “-?”0-9
A character in the range between “0” and “9”
Solution 2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher("test1string1337thingie");
// Find all matches
while (matcher.find()) {
// Get the matching string
String match = matcher.group();
}
Is one regexp solution.
Solution 3
try this code
String s = "qwerty1qwerty2";
for(int i=0;i<s.length();i++)
{
if(Character.isDigit(s.charAt(i)))
System.out.print(s.charAt(i)+" ");
}
Related videos on Youtube
Author by
user471011
Updated on April 29, 2021Comments
-
user471011 about 3 years
For example, I have input String: "qwerty1qwerty2";
As Output I would like have [1,2].
My current implementation below:
import java.util.ArrayList; import java.util.List; public class Test1 { public static void main(String[] args) { String inputString = args[0]; String digitStr = ""; List<Integer> digits = new ArrayList<Integer>(); for (int i = 0; i < inputString.length(); i++) { if (Character.isDigit(inputString.charAt(i))) { digitStr += inputString.charAt(i); } else { if (!digitStr.isEmpty()) { digits.add(Integer.parseInt(digitStr)); digitStr = ""; } } } if (!digitStr.isEmpty()) { digits.add(Integer.parseInt(digitStr)); digitStr = ""; } for (Integer i : digits) { System.out.println(i); } } }
But after double check I dislake couple points:
Some lines of code repeat twice.
I use List. I think it is not very good idea, better using array.
So, What do you think?
Could you please provide any advice?
-
Juvanis over 11 yearsyou are not allowed to use regex?
-
Hovercraft Full Of Eels over 11 yearsMy thoughts exactly as this would be pretty trivial using regular expressions and
String#replaceAll(...)
-
Dave Newton over 11 yearsFor code reviews please use the code review stack exchange site :)
-
Andrea Colleoni over 11 years
-
Toni over 11 yearsActually, let me check if the pattern is correct usage here, just quickly googled and improvised. Haven't used java nor regexp in a while.
-
Toni over 11 yearsYes, it matches. Just add results to some container of your choice.
-
user471011 over 11 yearsAnd what about this line: "123fgfgfgfgfgfgv5kkk60". Expected result is [123, 5, 60].
-
scanE over 11 yearsmodified code int len = s.length(); for(int i=0;i<len;i++) { if(Character.isDigit(s.charAt(i))) { System.out.print(s.charAt(i)); i++; while(i<len&&Character.isDigit(s.charAt(i))) { System.out.print(s.charAt(i)); i++; } System.out.print(" "); } }
-
Dave Newton over 11 years@user1831612 Edit your answer; don't try to stuff code into a comment--it's illegible.
-
Jelle de Fries almost 10 yearsYou should put
str = str.replaceAll("[^-?0-9]+", " ");
to match negative numbers. -
Maxim Shoustin almost 10 years@JelledeFries, well, who knows if
-
represented as minus or hyphen. -
Maxim Shoustin almost 10 years@JelledeFries I edited my answer with "-" support if needed
-
Jelle de Fries almost 10 years@MaximShoustin You are right, it depends on the problem. I had a problem that required the recognition of negative numbers.
-
kris123456 over 9 yearsBut the above solution doesn't seem to work for "0.0". Any suggestions on how to get this fixed too?
-
Maxim Shoustin over 9 years@kris123456 well, 0.0 is not number. suppose u need a pattern something like
[^-?0-9]+.[0-9]+