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?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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 executeyou 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.