+ 1
Split string Java. Please help!
Need to split string "New York 60 50 40 30" so it would be only integers part - "60 50 40 30"
15 Respuestas
+ 7
Here's another solution similar to Denise Roßberg's👍🍻
String s = "New York 60 50 40 30";
String filtered = s.replaceAll("[^0-9]", " ");
String[] numbers = filtered.split("\\s+");
for (String n : numbers) {
System.out.println(n);
}
+ 7
Ilja Veselov Here is, with No space before integers:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
String s = "New York 60 50 40 30";
Pattern pattern = Pattern.compile("\\d+");
Matcher match = pattern.matcher(s);
while (match.find()) {
System.out.print(match.group() +" ");
}
+ 6
Ilja Veselov you can post the code you're struggling with — https://www.sololearn.com/post/75089/?ref=app
+ 3
You can try something like this:
String str = "New York 60 50 40 30";
str = str.replaceAll("[A-Za-z]", "");
System.out.println(str);
Read more about regular expressions: https://www.vogella.com/tutorials/JavaRegularExpressions/article.html
+ 2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
Pattern pattern = Pattern.compile("\\d.+\\d");
Matcher matcher = pattern.matcher("New York 60 50 40 30");
String part = matcher.find() ? matcher.group() : "";
System.out.println( part );
+ 2
There also lessons about regular expressions here on sololearn:
https://www.sololearn.com/learn/9704/?ref=app
+ 1
Unfortunately this is not an answer to my question. Program is reading strings from a fila and I can't count chartAt index every time as all strings have different lengths . I have common pattern for all strings first location and then numbers so I need to split all strings to only numbes.
+ 1
Will try
+ 1
replaceAll method gives some spaces before integers it looks like : 60 50 40 30. Need no spaces before line.
+ 1
Here is the solution replaceAll("\\p{L}+\\s","")
+ 1
String s = "New York 60 50 40 30";
for(String t: s.split(" ")){
if(t.matches(".*\\d.*")){
System.out.println(t);
}
}
// this way works as well if you want like this
0
I know that it will be String.split, but how exactly?