I have a text which I need to read word by word till ';' delimiter... I have searched in many places and also i have read some chapters but I can't find any method to use... Please help!
string to read ie 32; Potatoe; Vegetables; 21.30; 12.20; 15 21 32 45;
String s = "32; Potatoe; Vegetables; 21.30; 12.20; 15 21 32 45;";
String[] splittedWords;
splittedWords = s.split(";");
You can use the method split to seperate words along delimiters. It will return a list of Strings. If you want to parse the values in the string to an Integer you can use this:
for (String string : splittedWords)
{
if(string.matches("[^a-z \\.]+")==true)
{
int value = Integer.parseInt(string);
System.out.println(value);
}
}
the only integer in your samplestring is 32, though. Thats why this code will only output "32".
Try this:
for ( String s : myOwnString.split(";") ){
System.out.println(s);
}
Java supports this very clearly, with the String.split()
method. You can read the documentation here .
String[] tokens = input.split(";");
// tokens contains each value.
You can use split
method of String
.
String string = "32; Potatoe; Vegetables; 21.30; 12.20; 15 21 32 45";
String[] split = string.split(";");
for(String s: split)
{
System.out.println(s);
}
This will print:
32
Potatoe
Vegetables
21.30
12.20
15 21 32 45
String text = "32; Potatoe; Vegetables; 21.30; 12.20; 15 21 32 45";
String[] words = new String[text.length()];
int initialIndex = 0,i=0;
while (initialIndex<text.length()) {
words[i] = text.substring(initialIndex, text.indexOf(";"));
i++;
initialIndex = text.indexOf(";")+1;
}
Now String of words contains all words in text. You can access by word.get(index);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.