9 Replies Latest reply: Nov 25, 2007 12:23 PM by 710081 RSS

    Split a string into many strings

    807603
      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
          Have you looked at the String class?
          • 2. Re: Split a string into many strings
            807603
            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
              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
                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
                  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
                    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
                      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
                        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
                          String iNeedSplit = "Bla bla blx";
                          String [] result = iNeedSplit.split(" ");
                          bla
                          bla
                          blx