I have a string containing several words. What i need is to seperate these words and add them to a Set<String>. How can I achieve that?
Printable View
I have a string containing several words. What i need is to seperate these words and add them to a Set<String>. How can I achieve that?
The String class has several methods that will help you get substrings from an existing string. See indexOf, substring and split for example. Also see the StringTokenizer class.
Tokenizer did the trick thanks :)
You can create a scanner object to operate on your string, then simply use the next() method to get words separated by whitespaces.
or you can use the method String.split(" "); to separate by white-spaces. It returns a String[].
Wouldn't that technically be tokenizing?
Ya, but you dont need the overhead of creating a StringTokenizer and all sort of crap like that. Plus you can split on things other than spaces. Like you can split on the letter a, or on commas, or on phrases, or on anything. I think you can do the same with StringTokenizers by setting delimiters and stuff. Regardless, I prefer how String.split(String) gives me a nice array of Strings to work with without me having to recurse through the String myself.
I use the same method, I was just saying that it's still tokenizing.
Run a timing test of StringTokenizer vs String.split() in a loop 10000 times. split can take much longer to executeQuote:
you dont need the overhead of creating a StringTokenizer
Whereas StringTokenizer splits up a String based upon a defined String using indexOf, the other techniques mentioned work based upon regular expressions. Hence, StringTokenizer would be expected to be faster in a situation where their efficiencies are pushed, but lack the ability to do more complex operations.