Splitting string based on delimiter - java

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

Related

Get two different delimiters from same string

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.

Java- Extract part of a string between two similar special characters

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]);

String Tokenizer (Double Quotes and Whitespace)

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

Regex Pattern to avoid : and , in the strings

I have a string which comes from the DB.
the string is something like this:-
ABC:def,ghi:jkl,hfh:fhgh,ahf:jasg
In short String:String, and it repeats for large values.
I need to parse this string to get only the words without any : or , and store each word in ArrayList
I can do it using split function(twice) but I figured out that using regex I can do it one go and get the arraylist..
String strLine="category:hello,good:bye,wel:come";
Pattern titlePattern = Pattern.compile("[a-z]");
Matcher titleMatcher = titlePattern.matcher(strLine);
int i=0;
while(titleMatcher.find())
{
i=titleMatcher.start();
System.out.println(strLine.charAt(i));
}
However it is not giving me proper results..It ends up giving me index of match found and then I need to append it which is not so logical and efficient,.
Is there any way around..
String strLine="category:hello,good:bye,wel:come";
String a[] = strLine.split("[,:]");
for(String s :a)
System.out.println(s);
Use java StringTokenizer
Sample:
StringTokenizer st = new StringTokenizer(in, ":,");
while(st.hasMoreTokens())
System.out.println(st.nextToken());
Even if you can use a regular expression to parse the entire string at once, I think it would be less readable than splitting it with multiple steps.

String Tokenizing in java

I need to tokenize a string using a delimiter.
StringTokenizer is capable of tokenizing the string with given delimiter. But, when there are two consecutive delimiters in the string, then it is not considering it as a token.
Thanks in advance for you help
Regards,
The second parameter to the constructor of StringTokenizer object is just a string containing all delimiters that you require.
StringTokenizer st = new StringTokenizer(str, "#!");
In this case, there are two delimiters both # and !
Consider this example :
String s = "Hello, i am using Stack Overflow;";
System.out.println("s = " + s);
String delims = " ,;";
StringTokenizer tokens = new StringTokenizer(s, delims);
while(tokens.hasMoreTokens())
System.out.println(tokens.nextToken());
Here you would get an output similar to this with 3 delimiters :
Hello
,
i
am
using
Stack
Overflow
;
Look into String.split()
This should do what you are looking for.
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
Use the split() method of java.lang.String and pass it a regular expression which matches your one or more delimiter condition.
for e.g. "a||b|||c||||d" could be tokenised with split("\\|{2,}"); with the resulting array [a,b,c,d]

Categories