Extract numbers from a string in java [duplicate] - java

This question already has answers here:
Extract digits from a string in Java
(14 answers)
Closed 4 years ago.
I want to extract numbers from a string like:
str="good_mor9ni13ng_23guys ";
The output should be an array [9,13,23].
How should I proceed?

You could do it like this:
List<String> matches = new ArrayList<String>();
Matcher m = Pattern.compile("[0-9]+").matcher("good_mor9ni13ng_23guys");
while (m.find()) {
matches.add(m.group());
}

Related

Getting substrings from a string in Java [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 2 years ago.
I have a string like this:
String ColorCodes = "colorcodes:#FFOOFF,#OOACOF,#EEAAAA"
I want to get the three color codes in the above string into three different strings, like this:
String ColorOne = "#FFOOFF"
String ColorTwo = "#OOACOF"
String ColorThree = "#EEAAAA"
How can I do this?
You can use substringand split:
String[] colors = colorCodes.substring(colorCodes.indexOf(":") + 1).split(",");
System.out.println(Arrays.toString(colors));
Output
[#FFOOFF, #OOACOF, #EEAAAA]

getting the string value before dot in string through java [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a string named extractDb values can be
GDSHKG.db
GDSMNH.db
GDSTKY.db
GDSLDN.db
now i want to remove the .extension partof it
that is i want to see the result as
GDSHKG
GDSMNH
GDSTKY
GDSLDN
please advise how to achieve this in java
Try to use substring like this:
String myString = "GDSHKG.db";
String newString = myString.substring(0, myString.indexOf("."));
System.out.print(newString);
Output:
GDSHKG

How to print only two digits after decimal with System.printf? [duplicate]

This question already has answers here:
String.split(".") is not splitting my long String
(2 answers)
Closed 8 years ago.
My problem is that double value converted to string cannot be splitted by dot.
Here you can see my code:
String valueOf = String.valueOf(12.34);
System.out.println("valueOf=" + valueOf);
String[] split = valueOf.split(".");
System.out.println("split=" + Arrays.toString(split));
The output is:
valueOf = 12.34
split = []
Why is split array empty?
You can try to run it on https://ideone.com/BBL4z2.
You need to escape . here. you can use \\.
String[] split = valueOf.split("\\.");
Because in regex you need to escape .

how to tokenize string with split() in java? [duplicate]

This question already has answers here:
How do I break a string into arguments, respecting quotes? [duplicate]
(2 answers)
Java: splitting a comma-separated string but ignoring commas in quotes
(12 answers)
Java splitting strings?
(4 answers)
Splitting on comma outside quotes
(5 answers)
Closed 8 years ago.
I need to tokenize this string.
AddItem rt456 4 12 BOOK "File Structures" "Addison-Wesley" "Michael Folk"
I need
"AddItem","rt456","12","BOOK","File Structures","Addison-Wesley","Michael Folk"
substrings. How i can get these tokens with using split()?
List<String> list = new ArrayList<String>();
Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str);
while (m.find())
list.add(m.group(1));
Output:
[AddItem, rt456, 4, 12, BOOK, "File Structures", "Addison-Wesley", "Michael Folk"]

Java split function [duplicate]

This question already has answers here:
split string on comma, but not commas inside parenthesis
(3 answers)
Closed 9 years ago.
Hi I have a question about java split
ItemName,Item,,(width,length,height),12
I want String[] equals this"
String[] result ={"ItemName","Item","","(width,length,height)","12"};
Who can help me with using split function or other regex?
If your input string is named istr, then
String[] result = istr.split(",");
should work.
try this
String data="ItemName,Item,,(width,length,height),12";
String[] result=data.split("//,");
for(String string : result) {
System.out.println(string);
}

Categories

Resources