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.

Results 1 to 10 of 10

Thread: How can i divide a string?

  1. #1
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can i divide a string?

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,246
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: How can i divide a string?

    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.

  3. #3
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i divide a string?

    Tokenizer did the trick thanks

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How can i divide a string?

    You can create a scanner object to operate on your string, then simply use the next() method to get words separated by whitespaces.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,706
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: How can i divide a string?

    or you can use the method String.split(" "); to separate by white-spaces. It returns a String[].

  6. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: How can i divide a string?

    Wouldn't that technically be tokenizing?

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,706
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: How can i divide a string?

    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.

  8. #8
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: How can i divide a string?

    I use the same method, I was just saying that it's still tokenizing.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,246
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: How can i divide a string?

    you dont need the overhead of creating a StringTokenizer
    Run a timing test of StringTokenizer vs String.split() in a loop 10000 times. split can take much longer to execute

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,369
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: How can i divide a string?

    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.

Similar Threads

  1. How can I convert a String to Set<String>? Is it possible?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 25th, 2010, 10:03 AM
  2. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 11:16 AM
  3. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM