How can I use split function in java using two delimiters in the same string
I want to get the words with commas and spaces separately
String I = "hello,hi hellow,bye"
I want to get the above string splited as
String var1 = hello,bye
String var2 = hi hellow
Any suggestion is very much valued.
I would try to first split them with one of the delimiters, then for each resulting substring split with the other delimiter.
Related
Java- Extract part of a string between two similar special characters.
I want to substring the second number, example :
String str = '1-10-251';
I want the result to be: 10
String str = "1-10-251";
String[] strArray = str.split("-");
System.out.println(strArray[1]);
I am trying to implement a way for taking in arguments for a photo album that I am building. However, I am having a hard time figuring out how to tokenize the input.
Two sample inputs:
addPhoto "DSC_017.jpg" "DSC_017" "Fall colors"
addPhoto "DSC_018.jpg" "DSC_018" "Colorado Springs"
I would like this input to return a String array containing 4 elements where
String s[1]="addPhoto"
String s[2]="DSC_017.jpg"
String s[3]="DSC_017"
String s[4] = "Fall colors"
I looked into StringTokenizer and String.split but I'm not sure how to go about setting the delimiters.
String line = "addPhoto \"DSC_018.jpg\" \"DSC_018\" \"Colorado Springs\"";
String[] pieces = line.split(" \"");
for (String p : pieces) {
System.out.println(p.replaceAll("\"", ""));
}
You might want to pull these arguments off the command line args, the shell will do the quote handling for you. However, you'll only be able to do one addPhoto operation at a time.
If you can't do that, you might try one of these answers:
http://www.source-code.biz/snippets/java/5.htm
Parsing quoted text in java
Split a quoted string with a delimiter
Tokenizing a String but ignoring delimiters within quotes
A string is taken as input which is in the form of 23,4,555,67 via deadline nd another input is key yo search the element linearly ?my question is how can we recognize the elements from string separated by comma
You can split the String using split :
String[] tokens = "23,4,555,67".split(",");
String s = "23,4,555,67"
String[] tokens = s.split(",");
This will give you a string array with the numbers.
Alternatively, you can use a StringTokenizer. (java.util)
This can be used if your string is delimited by more than one characters (can be be used as well in case of single character). Your example using StringTokenizer
SrringTokenizer st = new StringTokenizer("23,4,555,67", ",");
while(st.hasMoreElements())
System.out.println(st.nextToken());
I have been given a text from inputstream and first I put it into String via StringBuilder.
Then I want to split the text(now string,since it's not from inputstream) in words, but in some places in the text there are not just one space, but more spaces between the words and interpunct symbols. Programming language should be JAVA.
A simple solution would be using the String split method:
String[] words = myString.split(" ");
If you have a StringBuilder instead of a String:
String[] words = myStringBuilder.toString().split(" ");
If you would like to split by any whitespace characters, use How do I split a string with any whitespace chars as delimiters?
myString.split("\\s+");
Suppose I want to split a string by either space character or the %20 string, how should I write my regex?
I tried the following, but it didn't work.
String regex = "[\\s+, %20]";
String str1 = "abc%20xyz";
String str2 = "abc xyz";
str1.split(regex);
str2.split(regex);
The regex doesn't seem to work on str1.
use the alternation |:
String regex = "(?:\\s+|%20)+";
String regex = "(\\s{1}+|%20{1}+)";
If you want to split by ONE space or ONE "%20", try this:
String regex = "(\\s|%20)";
If you want to split by AT LEAST ONE space or AT LEAST ONE "%20", then try this:
String regex = "(\\s+|(%20)+)";