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
Related
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.
I am attempting to split a String into 2 separate Strings, one from the first letter up until a tab, and the other beginning after the tab and ending at the end of the String. I have looked over this post and have found my problem to be different. I am currently trying to utilize the split() method, but with no luck. My code is as follows:
Scanner loadFile = new Scanner(System.in);
loadFile = new Scanner(menuFile);
//loops through data and adds into the SSST
while(loadFile.hasNextLine()){
String line = loadFile.nextLine();
String[] thisLine = line.split(" ");
System.out.println(thisLine[0]);
String item = thisLine[0];
String value = thisLine[1];
menu.put(item, value);
I run into my problem at the line line.split(" "); because I do not know the argument to provide to this method in order to split at the tab in my String.
menu in this code is a separate object and is irrelevant.
Sample input for this program:
"baguette 400"
Desired output for this program:
String 1: "baguette"
String 2: "400"
The tab character is written \t. The code for splitting the line thus looks like this:
String[] thisLine = line.split("\t");
More flexible, if feasible for your use case: For splitting on generic white space characters, including space and tab use \\s (note the double reversed slash, because this is a regex).
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 am having a difficult time figuring out how to split a string like the one following:
String str = "hi=bye,hello,goodbye,pickle,noodle
This string was read from a text file and I need to split the string into each element between the commas. So I would need to split each element into their own string no matter what the text file reads. Keep in mind, each element could be any length and there could be any amount of elements which 'hi' is equal to. Any ideas? Thanks!
use split!
String[] set=str.split(",");
then access each string as you need from set[...] (so lets say you want the 3rd string, you would say: set[2]).
As a test, you can print them all out:
for(int i=0; i<set.length;i++){
System.out.println(set[i]);
}
If you need a bit more advanced approach, I suggest guava's Splitter class:
Iterable<String> split = Splitter.on(',')
.omitEmptyStrings()
.trimResults()
.split(" bye,hello,goodbye,, , pickle, noodle ");
This will get rid of leading or trailing whitespaces and omit blank matches. The class has some more cool stuff in it like splitting your String into key/value pairs.
str = str.subString(indexOf('=')+1); // remove "hi=" part
String[] set=str.split(",");
I'm wondering: Do you mean to split it as such:
"hi=bye"
"hi=hello"
"hi=goodbye"
"hi=pickle"
"hi=noodle"
Because a simple split(",") will not do this. What's the purpose of having "hi=" in your given string?
Probably, if you mean to chop hi= from the front of the string, do this instead:
String input = "hi=bye,hello,goodbye,pickle,noodle";
String hi[] = input.split(",");
hi[0] = (hi[0].split("="))[1];
for (String item : hi) {
System.out.println(item);
}
I'm trying to split paragraphs of information from an array into a new one which is broken into individual words. I know that I need to use the String[] split(String regex), but I can't get this to output right.
What am I doing wrong?
(assume that sentences[i] is the existing array)
String phrase = sentences[i];
String[] sentencesArray = phrase.split("");
System.out.println(sentencesArray[i]);
Thanks!
It might be just the console output going wrong. Try replacing the last line by
System.out.println(java.util.Arrays.toString(sentencesArray));
The empty-string argument to phrase.split("") is suspect too. Try passing a word boundary:
phrase.split("\\b");
You are using an empty expression for splitting, try phrase.split(" ") and work from there.
This does nothing useful:
String[] sentencesArray = phrase.split("");
you're splitting on empty string and it will return an array of the individual characters in the string, starting with an empty string.
It's hard to tell from your question/code what you're trying to do but if you want to split on words you need something like:
private static final Pattern SPC = Pattern.compile("\\s+");
.
.
String[] words = SPC.split(phrase);
The regex will split on one or more spaces which is probably what you want.
String[] sentencesArray = phrase.split("");
The regex based on which the phrase needs to be split up is nothing here. If you wish to split it based on a space character, use:
String[] sentencesArray = phrase.split(" ");
// ^ Give this space