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

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"]

Related

How do I replace the " character in Java? [duplicate]

This question already has answers here:
What are all the escape characters?
(5 answers)
Closed 2 years ago.
I read a line from a txt file containing a string = "abc,"".
If one wanted to replace string to: string = "abc", one would write
string = string.replace(",",""); to replace the comma, but how would one replace the "?
Problem:
string = string.replace(""","");//code does not work because of """
You need to escape the ", so like this:
string = string.replace("\"","")

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

Extract numbers from a string in java [duplicate]

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());
}

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 .

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