Splitting string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Splitting string

    Hi

    I have a multi-line address field which has each line separated by CRLF. How
    can I split this field into individual strings using crlf as separator?

    Thanks

    Regards


  • sloan

    #2
    Re: Splitting string


    String.Split ?

    http://msdn2.microsoft.com/en-us/lib...ing.split.aspx

    "John" <John@nospam.in fovis.co.ukwrot e in message
    news:OPxWIVZOHH A.992@TK2MSFTNG P06.phx.gbl...
    Hi
    >
    I have a multi-line address field which has each line separated by CRLF.
    How
    can I split this field into individual strings using crlf as separator?
    >
    Thanks
    >
    Regards
    >
    >

    Comment

    • Scott M.

      #3
      Re: Splitting string

      dim result As String() = theString.Split (Chr(13))


      "sloan" <sloan@ipass.ne twrote in message
      news:%235g9AmZO HHA.2232@TK2MSF TNGP02.phx.gbl. ..
      >
      String.Split ?
      >
      http://msdn2.microsoft.com/en-us/lib...ing.split.aspx
      >
      "John" <John@nospam.in fovis.co.ukwrot e in message
      news:OPxWIVZOHH A.992@TK2MSFTNG P06.phx.gbl...
      >Hi
      >>
      >I have a multi-line address field which has each line separated by CRLF.
      How
      >can I split this field into individual strings using crlf as separator?
      >>
      >Thanks
      >>
      >Regards
      >>
      >>
      >
      >

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Splitting string

        "John" <John@nospam.in fovis.co.ukschr ieb:
        I have a multi-line address field which has each line separated by CRLF.
        How can I split this field into individual strings using crlf as
        separator?
        'Microsoft.Visu alBasic.Strings .Split' + 'ControlChars.C rLf' is what you are
        looking for.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Stephany Young

          #5
          Re: Splitting string

          Don't you mean?:

          Dim result As String() = theString.Split (New String()
          {Environment.Ne wLine})


          "Scott M." <s-mar@nospam.nosp amwrote in message
          news:Oj6XihcOHH A.5104@TK2MSFTN GP06.phx.gbl...
          dim result As String() = theString.Split (Chr(13))
          >
          >
          "sloan" <sloan@ipass.ne twrote in message
          news:%235g9AmZO HHA.2232@TK2MSF TNGP02.phx.gbl. ..
          >>
          >String.Split ?
          >>
          >http://msdn2.microsoft.com/en-us/lib...ing.split.aspx
          >>
          >"John" <John@nospam.in fovis.co.ukwrot e in message
          >news:OPxWIVZOH HA.992@TK2MSFTN GP06.phx.gbl...
          >>Hi
          >>>
          >>I have a multi-line address field which has each line separated by CRLF.
          >How
          >>can I split this field into individual strings using crlf as separator?
          >>>
          >>Thanks
          >>>
          >>Regards
          >>>
          >>>
          >>
          >>
          >
          >

          Comment

          • Scott M.

            #6
            Re: Splitting string

            No, I don't.


            "Stephany Young" <noone@localhos twrote in message
            news:%23nkYfHdO HHA.1248@TK2MSF TNGP02.phx.gbl. ..
            Don't you mean?:
            >
            Dim result As String() = theString.Split (New String()
            {Environment.Ne wLine})
            >
            >
            "Scott M." <s-mar@nospam.nosp amwrote in message
            news:Oj6XihcOHH A.5104@TK2MSFTN GP06.phx.gbl...
            >dim result As String() = theString.Split (Chr(13))
            >>
            >>
            >"sloan" <sloan@ipass.ne twrote in message
            >news:%235g9AmZ OHHA.2232@TK2MS FTNGP02.phx.gbl ...
            >>>
            >>String.Spli t ?
            >>>
            >>http://msdn2.microsoft.com/en-us/lib...ing.split.aspx
            >>>
            >>"John" <John@nospam.in fovis.co.ukwrot e in message
            >>news:OPxWIVZO HHA.992@TK2MSFT NGP06.phx.gbl.. .
            >>>Hi
            >>>>
            >>>I have a multi-line address field which has each line separated by
            >>>CRLF.
            >>How
            >>>can I split this field into individual strings using crlf as separator?
            >>>>
            >>>Thanks
            >>>>
            >>>Regards
            >>>>
            >>>>
            >>>
            >>>
            >>
            >>
            >
            >

            Comment

            • Stephany Young

              #7
              Re: Splitting string

              Well, seeing as how the OP has a string with CRLF's in it and want's to
              split it on CRLF, how is:

              dim result As String() = theString.Split (Chr(13))

              going to do that?.

              It is going to split the string on CR and leave the LF character at the
              beginning of the 2nd and subsequent elements of the resulting array.


              "Scott M." <s-mar@nospam.nosp amwrote in message
              news:%23t8suodO HHA.4244@TK2MSF TNGP04.phx.gbl. ..
              No, I don't.
              >
              >
              "Stephany Young" <noone@localhos twrote in message
              news:%23nkYfHdO HHA.1248@TK2MSF TNGP02.phx.gbl. ..
              >Don't you mean?:
              >>
              > Dim result As String() = theString.Split (New String()
              >{Environment.N ewLine})
              >>
              >>
              >"Scott M." <s-mar@nospam.nosp amwrote in message
              >news:Oj6XihcOH HA.5104@TK2MSFT NGP06.phx.gbl.. .
              >>dim result As String() = theString.Split (Chr(13))
              >>>
              >>>
              >>"sloan" <sloan@ipass.ne twrote in message
              >>news:%235g9Am ZOHHA.2232@TK2M SFTNGP02.phx.gb l...
              >>>>
              >>>String.Spl it ?
              >>>>
              >>>http://msdn2.microsoft.com/en-us/lib...ing.split.aspx
              >>>>
              >>>"John" <John@nospam.in fovis.co.ukwrot e in message
              >>>news:OPxWIVZ OHHA.992@TK2MSF TNGP06.phx.gbl. ..
              >>>>Hi
              >>>>>
              >>>>I have a multi-line address field which has each line separated by
              >>>>CRLF.
              >>>How
              >>>>can I split this field into individual strings using crlf as
              >>>>separator ?
              >>>>>
              >>>>Thanks
              >>>>>
              >>>>Regards
              >>>>>
              >>>>>
              >>>>
              >>>>
              >>>
              >>>
              >>
              >>
              >
              >

              Comment

              • Phill W.

                #8
                Re: Splitting string

                John wrote:
                I have a multi-line address field which has each line separated by CRLF. How
                can I split this field into individual strings using crlf as separator?
                For VB 2003, you /have/ to use

                Microsoft.Visua lBasic.Strings. Split( s, vbCrLf )

                Dim x as String _
                = "X,Y,Z".Replace ( ".", vbCrlf )

                ? Microsoft.Visua lBasic.Strings. Split( x, vbCrLf ).Length
                3

                The String.Split method /does not/ cater for multiple-character
                delimiters, only working with lists of characters, each of which it
                treats as a separate delimiter, leaving you with lots of blank entries
                in the resulting array).

                ? String.Split( x, vbCr, vbLf ).Length
                5

                VB 2005 has a [more] sensible String.Split method that can handle
                multiple-character delimiters.

                ? String.Split( x, New String() { vbCrLf } ).Length
                3

                HTH,
                Phill W.

                Comment

                • Scott M.

                  #9
                  Re: Splitting string

                  Yes, I see. I had thought that Chr(13) would get the entire CRLF.

                  But, in your suggestion, the "New String()" is not needed as the Split
                  method returns an implied string array anyway. So, it seems the best choice
                  is a combination of our suggestions:

                  Dim result As String() = theString.Split (Environment.Ne wLine)


                  "Stephany Young" <noone@localhos twrote in message
                  news:OFbZTSfOHH A.4644@TK2MSFTN GP03.phx.gbl...
                  Well, seeing as how the OP has a string with CRLF's in it and want's to
                  split it on CRLF, how is:
                  >
                  dim result As String() = theString.Split (Chr(13))
                  >
                  going to do that?.
                  >
                  It is going to split the string on CR and leave the LF character at the
                  beginning of the 2nd and subsequent elements of the resulting array.
                  >
                  >
                  "Scott M." <s-mar@nospam.nosp amwrote in message
                  news:%23t8suodO HHA.4244@TK2MSF TNGP04.phx.gbl. ..
                  >No, I don't.
                  >>
                  >>
                  >"Stephany Young" <noone@localhos twrote in message
                  >news:%23nkYfHd OHHA.1248@TK2MS FTNGP02.phx.gbl ...
                  >>Don't you mean?:
                  >>>
                  >> Dim result As String() = theString.Split (New String()
                  >>{Environment. NewLine})
                  >>>
                  >>>
                  >>"Scott M." <s-mar@nospam.nosp amwrote in message
                  >>news:Oj6XihcO HHA.5104@TK2MSF TNGP06.phx.gbl. ..
                  >>>dim result As String() = theString.Split (Chr(13))
                  >>>>
                  >>>>
                  >>>"sloan" <sloan@ipass.ne twrote in message
                  >>>news:%235g9A mZOHHA.2232@TK2 MSFTNGP02.phx.g bl...
                  >>>>>
                  >>>>String.Spli t ?
                  >>>>>
                  >>>>http://msdn2.microsoft.com/en-us/lib...ing.split.aspx
                  >>>>>
                  >>>>"John" <John@nospam.in fovis.co.ukwrot e in message
                  >>>>news:OPxWIV ZOHHA.992@TK2MS FTNGP06.phx.gbl ...
                  >>>>>Hi
                  >>>>>>
                  >>>>>I have a multi-line address field which has each line separated by
                  >>>>>CRLF.
                  >>>>How
                  >>>>>can I split this field into individual strings using crlf as
                  >>>>>separato r?
                  >>>>>>
                  >>>>>Thanks
                  >>>>>>
                  >>>>>Regards
                  >>>>>>
                  >>>>>>
                  >>>>>
                  >>>>>
                  >>>>
                  >>>>
                  >>>
                  >>>
                  >>
                  >>
                  >
                  >

                  Comment

                  • Stephany Young

                    #10
                    Re: Splitting string

                    That would have been cool, Scott, except that in their infinite wisdom, the
                    powers that be omitted a suitable overload that can take just a string as
                    the delimiter.

                    A single char works because the String.Split(ch ar()) overload takes a
                    ParamArray which can be 1 char or many seperate char but if you feed it
                    String.Split(Ch rW(13), Chr(10)) it wouldn't treat the 2 characters as a
                    combined entity. Instaed it would split on either CR or LF and still give
                    incorrect results for this case.

                    The first overload that takes a string, actually takes an Array of strings
                    (and not a ParamArray) and so the usage actually needs to be, (in this
                    case):

                    Dim result As String() = theString.Split (New String()
                    {Environment.Ne wLine}, StringSplitOpti ons.None)

                    or:

                    Dim result As String() = theString.Split (New String()
                    {Environment.Ne wLine}, StringSplitOpti ons.RemoveEmpty Entries)

                    Unfortunately, in my haste to point out your error, I still got it wrong in
                    my first post to this thread. (Consider my wrist slapped.)


                    "Scott M." <s-mar@nospam.nosp amwrote in message
                    news:eiuCELoOHH A.2232@TK2MSFTN GP02.phx.gbl...
                    Yes, I see. I had thought that Chr(13) would get the entire CRLF.
                    >
                    But, in your suggestion, the "New String()" is not needed as the Split
                    method returns an implied string array anyway. So, it seems the best
                    choice is a combination of our suggestions:
                    >
                    Dim result As String() = theString.Split (Environment.Ne wLine)
                    >
                    >
                    "Stephany Young" <noone@localhos twrote in message
                    news:OFbZTSfOHH A.4644@TK2MSFTN GP03.phx.gbl...
                    >Well, seeing as how the OP has a string with CRLF's in it and want's to
                    >split it on CRLF, how is:
                    >>
                    > dim result As String() = theString.Split (Chr(13))
                    >>
                    >going to do that?.
                    >>
                    >It is going to split the string on CR and leave the LF character at the
                    >beginning of the 2nd and subsequent elements of the resulting array.
                    >>
                    >>
                    >"Scott M." <s-mar@nospam.nosp amwrote in message
                    >news:%23t8suod OHHA.4244@TK2MS FTNGP04.phx.gbl ...
                    >>No, I don't.
                    >>>
                    >>>
                    >>"Stephany Young" <noone@localhos twrote in message
                    >>news:%23nkYfH dOHHA.1248@TK2M SFTNGP02.phx.gb l...
                    >>>Don't you mean?:
                    >>>>
                    >>> Dim result As String() = theString.Split (New String()
                    >>>{Environment .NewLine})
                    >>>>
                    >>>>
                    >>>"Scott M." <s-mar@nospam.nosp amwrote in message
                    >>>news:Oj6Xihc OHHA.5104@TK2MS FTNGP06.phx.gbl ...
                    >>>>dim result As String() = theString.Split (Chr(13))
                    >>>>>
                    >>>>>
                    >>>>"sloan" <sloan@ipass.ne twrote in message
                    >>>>news:%235g9 AmZOHHA.2232@TK 2MSFTNGP02.phx. gbl...
                    >>>>>>
                    >>>>>String.Spl it ?
                    >>>>>>
                    >>>>>http://msdn2.microsoft.com/en-us/lib...ing.split.aspx
                    >>>>>>
                    >>>>>"John" <John@nospam.in fovis.co.ukwrot e in message
                    >>>>>news:OPxWI VZOHHA.992@TK2M SFTNGP06.phx.gb l...
                    >>>>>>Hi
                    >>>>>>>
                    >>>>>>I have a multi-line address field which has each line separated by
                    >>>>>>CRLF.
                    >>>>>How
                    >>>>>>can I split this field into individual strings using crlf as
                    >>>>>>separator ?
                    >>>>>>>
                    >>>>>>Thanks
                    >>>>>>>
                    >>>>>>Regards
                    >>>>>>>
                    >>>>>>>
                    >>>>>>
                    >>>>>>
                    >>>>>
                    >>>>>
                    >>>>
                    >>>>
                    >>>
                    >>>
                    >>
                    >>
                    >
                    >

                    Comment

                    • Cor Ligthert [MVP]

                      #11
                      Re: Splitting string

                      Don't you mean?:
                      >
                      Dim result As String() = theString.Split (New String()
                      {Environment.Ne wLine})
                      >
                      >
                      No that is for hairs,

                      :-)

                      Cor


                      Comment

                      • Cor Ligthert [MVP]

                        #12
                        Re: Splitting string

                        John,

                        I assume that you have a multiline field where the line itself is broken by
                        CRLFCRLF.

                        What I do than is first replace all CRLFCRLF by an impossible
                        charachterrange and than make from the CRLF a csv seperator character
                        depending on your country proceded and ended with a ", than I change the
                        impossible charachter back to
                        """CRLFCRLF """ this sets than the needed starting and ending " as well.

                        Cor


                        Comment

                        • Bruce W. Darby

                          #13
                          Re: Splitting string


                          "Stephany Young" <noone@localhos twrote in message
                          news:uwv1yYoOHH A.3268@TK2MSFTN GP03.phx.gbl...
                          Unfortunately, in my haste to point out your error, I still got it wrong
                          in my first post to this thread. (Consider my wrist slapped.)
                          *sigh* And never when I'm there... LOL


                          Comment

                          Working...