This discussion is archived
9 Replies Latest reply: Nov 25, 2007 10:23 AM by 710081 RSS

Split a string into many strings

807603 Newbie
Currently Being Moderated
Hi there,

What i am trying to do is split up a string into many stringgs

eg: String:This man is extremely happy

String:This
String:man
String:is
String:extremely
String:happy

How can this be done, possibly using a class, a method?

Thanks in advance

Edited by: vertozia on Nov 23, 2007 4:37 PM
  • 1. Re: Split a string into many strings
    807603 Newbie
    Currently Being Moderated
    Have you looked at the String class?
  • 2. Re: Split a string into many strings
    807603 Newbie
    Currently Being Moderated
    go to this website:
    {color:0000ff}_http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html_{color}

    hit ctrl-f
    in your browsers "find" dialog type: "split"

    in case you ever happen to need it, the regex (regular expression)
    for whitespace is:
    "\\s+"
  • 3. Re: Split a string into many strings
    807603 Newbie
    Currently Being Moderated
    eah, i came across substring, but for that you have to index the string, i want it to automatically detect separate words by finding spaces
  • 4. Re: Split a string into many strings
    807603 Newbie
    Currently Being Moderated
    vertozia wrote:
    eah, i came across substring,
    wow. then you really didnt look hard enough for a method to SPLIT a string.
  • 5. Re: Split a string into many strings
    807603 Newbie
    Currently Being Moderated
    by the way, if you had just googled "java split string" the first link is an example

    {color:0000ff}_http://www.google.com/search?hl=en&q=java+split+string_{color}
    {color:0000ff}_http://www.rgagnon.com/javadetails/java-0438.html_{color}
  • 6. Re: Split a string into many strings
    807603 Newbie
    Currently Being Moderated
    wow, you guys were all really helpful, here comes my last question. how do i save them in separate strings?

    Thank you very much
  • 7. Re: Split a string into many strings
    807603 Newbie
    Currently Being Moderated
    EAZY

    First, split the string. Then call System.out.println( array.length );
    After you find out the length, close that, start up a new java program.
    Call it whatever you want.
    Now, just create however many strings you need :)

    For example, after you split the string and it created an array of length 5.
    Then, create a file
    and add
    String s1 = position 0 of array.
    String s2 = position 1 of array.
    String s3 = position 2 of array.
    String s4 = position 3 of array.
    String s5 = position 4 of array.
    ....
    String s99 = position 98 of array.
    It's that simple.
  • 8. Re: Split a string into many strings
    807603 Newbie
    Currently Being Moderated
    Hello,
    public static void main(String[] args) throws FileNotFoundException, IOException {
            
            String str = "This man is extremely happy";
            StringTokenizer st = new StringTokenizer(str," ");
            
            ArrayList al = new ArrayList();
            
            while (st.hasMoreTokens()){
                al.add(st.nextToken());
            }
            
            for (int i=0 ; i<al.size() ; i++) {
                System.out.println(al.get(i));
            }
            
        }
    java.util.StringTokenizer will split a string based on an arbitrary delimeter (in this case " ").
    java.util.ArrayList is a dynamic data structure that you can easily use to add/use objects.

    Good Luck,
    -Sam
  • 9. Re: Split a string into many strings
    710081 Newbie
    Currently Being Moderated
    String iNeedSplit = "Bla bla blx";
    String [] result = iNeedSplit.split(" ");
    bla
    bla
    blx