12 Replies Latest reply: Jul 13, 2007 10:40 AM by 807605 RSS

    best way to break down a string?

    807605
      Hi.

      I am wondering, what is the best way to get all the substrings with a patten from a string.

      Here is an example.

      String str = "|hello|123456|a,b,c,d,e,|"

      i want to get all the substrings between 2 consecutive "|" symbols.

      hello
      123456
      a,b,c,d,e,

      is there a build in method to achieve it? Read it char by char is the only way?

      what do you think is most efficient way of doing it?
        • 1. Re: best way to break down a string?
          JosAH
          Have a look at the String.split() method.

          kind regards,

          Jos
          • 2. Re: best way to break down a string?
            807605
            Have a look at the String.split() method.

            kind regards,

            Jos
            :-)))) When was your 'Road to Damascus' conversion Jos?
            • 3. Re: best way to break down a string?
              807605
              Hi.

              I am wondering, what is the best way to get all the
              substrings with a patten from a string.

              Here is an example.

              String str = "|hello|123456|a,b,c,d,e,|"

              i want to get all the substrings between 2
              consecutive "|" symbols.

              hello
              123456
              a,b,c,d,e,

              is there a build in method to achieve it? Read it
              char by char is the only way?

              what do you think is most efficient way of doing it?
              i would use the substring command and look for the index's of the characters you wan't it saves a lot of space compared to the charAt command.. something kinda like this(you prob need to fix it up some)

              )
              String str = "|hello|123456|a,b,c,d,e,|"
              int index = 0
              while (front < str.LastindexOf("|")
              {
                    frontindex = str.indexOf("|",front)
                    backindex = str.indexOf("|",front+1)
                    newstr[index] = str.substring (front, back)
              index++
              }
              • 4. Re: best way to break down a string?
                807605
                i would use the substring command and look for the
                index's of the characters you wan't it saves a lot of
                space compared to the charAt command.. something
                kinda like this(you prob need to fix it up some)

                )
                String str = "|hello|123456|a,b,c,d,e,|"
                int index = 0
                while (front < str.LastindexOf("|")
                {
                frontindex = str.indexOf("|",front)
                backindex = str.indexOf("|",front+1)
                newstr[index] = str.substring (front, back)
                +
                }
                I find it difficult to get enthusiastic about about using a loop like this when the OP can simply use
                String[] splitLine = line.split("\\|");
                • 5. Re: best way to break down a string?
                  JosAH
                  Have a look at the String.split() method.

                  kind regards,

                  Jos
                  :-)))) When was your 'Road to Damascus' conversion Jos?
                  Hi Sabre, how's going?

                  I've never been to Damascus and I still thank god on my bare knees for
                  being an atheist ;-)

                  kind regards,

                  Jos
                  • 6. Re: best way to break down a string?
                    807605
                    hehe true forgot about the split... actually that just helped me simplify a program i have lol
                    • 7. Re: best way to break down a string?
                      807605
                      Have a look at the String.split() method.

                      kind regards,

                      Jos
                      :-)))) When was your 'Road to Damascus' conversion
                      Jos?

                      Hi Sabre, how's going?
                      It goes well Jos. I'm playing a lot of Bridge and if we ever get any weather that is half decent I shall spend most of my day cycling and my evenings playing Bridge.

                      >
                      I've never been to Damascus and I still thank god on
                      my bare knees for
                      being an atheist ;-)
                      :-)
                      • 8. Re: best way to break down a string?
                        JosAH
                        Hi Sabre, how's going?
                        It goes well Jos. I'm playing a lot of Bridge and if we ever get any
                        weather that is half decent I shall spend most of my day cycling and
                        my evenings playing Bridge.
                        The forecasts predict hot weather this weekend; I suspect it'll be raining
                        hot water as well ...

                        What is it with you and that profane Bridge game ;-)

                        kind regards,

                        Jos (<--- growing gills)
                        • 9. Re: best way to break down a string?
                          807605
                          What is it with you and that profane Bridge game ;-)
                          I drink it instead of Grolsch. I get tipsey on it and sometimes drive erratically after playing but it does not put me over the legal limit of anything. I certainly don't have blind spots for concrete walls after I have played Bridge.

                          >
                          kind regards,

                          Jos (<--- growing gills)
                          I'm please that I live at the highest point in Huntingdonshire (90 m) so I don't yet need gills but the people at the bottom of the hill are all building boats.
                          • 10. Re: best way to break down a string?
                            DrClap
                            I'm please that I live at the highest point in Huntingdonshire (90 m)
                            Wikipedia claims it's 80m, not 90m.

                            http://en.wikipedia.org/wiki/List_of_historic_counties_of_England_and_Wales_by_highest_point

                            Does the page need editing or is your abode on a tall platform? And is it really called "Boring Field" or was that some Wikipedian's idea of a joke?
                            • 11. Re: best way to break down a string?
                              807605
                              I'm please that I live at the highest point in
                              Huntingdonshire (90 m)

                              Wikipedia claims it's 80m, not 90m.
                              I live on the top of the church tower!

                              >
                              http://en.wikipedia.org/wiki/List_of_historic_counties
                              ofEngland_and_Wales_by_highest_point

                              Does the page need editing or is your abode on a tall
                              platform? And is it really called "Boring Field" or
                              was that some Wikipedian's idea of a joke?
                              Cambridgeshire and Lincolnshire can be described as "Boring Field"s since they are only slightly less boringly flat than Holland. Huntingdonshire, the home of Oliver Cromwell and Samuel Pepys, is better described as 'lifeless'.
                              • 12. Re: best way to break down a string?
                                JosAH
                                Cambridgeshire and Lincolnshire can be described as
                                "Boring Field"s since they are only slightly less
                                boringly flat than Holland. Huntingdonshire, the home
                                of Oliver Cromwell and Samuel Pepys, is better
                                described as 'lifeless'.
                                Oh dear, drinking all that Bridge stuff must've gotten to you ...

                                kind regards,

                                Jos (<--- the Dutch Mountain Man)