string.split only accept char[] separator

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

    #1

    string.split only accept char[] separator

    Hi,

    I have a string that needs to be parsed into the string[]. The separator is
    not char[]. It is something like " at ". With current string.Split function,
    it doesn't work. Is there any exist functions like "Split" that I can use to
    do the job? Any idea of the simpliest way to do the job?

    Thanks in advance,
    Henry


  • Klaus H. Probst

    #2
    Re: string.split only accept char[] separator

    String.Split is designed to be pretty basic. You need to use something like
    a regex here. For example, given the string

    "one::two::thre e::four::five:: six"

    this expression:

    \w+[^::]

    will give you six matches, each with one number.

    There's probably a better way (Regex object have a Split method as well),
    but you get the idea.


    --
    _______________ _____
    Klaus H. Probst, MVP


    "Henry Chen" <henry.chen@pos tiive-it.com.au> wrote in message
    news:eIJSmUIuDH A.2308@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hi,
    >
    > I have a string that needs to be parsed into the string[]. The separator[/color]
    is[color=blue]
    > not char[]. It is something like " at ". With current string.Split[/color]
    function,[color=blue]
    > it doesn't work. Is there any exist functions like "Split" that I can use[/color]
    to[color=blue]
    > do the job? Any idea of the simpliest way to do the job?
    >
    > Thanks in advance,
    > Henry
    >
    >[/color]


    Comment

    • Chris R. Timmons

      #3
      Re: string.split only accept char[] separator

      "Henry Chen" <henry.chen@pos tiive-it.com.au> wrote in
      news:eIJSmUIuDH A.2308@TK2MSFTN GP11.phx.gbl:
      [color=blue]
      > Hi,
      >
      > I have a string that needs to be parsed into the string[]. The
      > separator is not char[]. It is something like " at ". With
      > current string.Split function, it doesn't work. Is there any
      > exist functions like "Split" that I can use to do the job? Any
      > idea of the simpliest way to do the job?[/color]

      Henry,

      Use the Regex.Split function (found in the
      System.Text.Reg ularExpressions namespace).

      Hope this helps.

      Chris.
      -------------
      C.R. Timmons Consulting, Inc.

      Comment

      • Scott Carter

        #4
        Re: string.split only accept char[] separator

        See string.ToCharAr ray(). For performance and simplicity, I'd recommend
        string.Split(.. .) over RegEx.

        Scott
        [color=blue]
        > I have a string that needs to be parsed into the string[]. The separator[/color]
        is[color=blue]
        > not char[]. It is something like " at ". With current string.Split[/color]
        function,[color=blue]
        > it doesn't work. Is there any exist functions like "Split" that I can use[/color]
        to[color=blue]
        > do the job? Any idea of the simpliest way to do the job?[/color]


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: string.split only accept char[] separator

          Henry,
          There are three Split functions in .NET:

          Use Microsoft.Visua lBasic.Strings. Split if you need to split a string based
          on a specific word (string). It is the Split function from VB6.

          Use System.String.S plit if you need to split a string based on a collection
          of specific characters. Each individual character is its own delimiter.

          Use System.Text.Reg ularExpressions .RegEx.Split to split based
          on matching patterns.

          If you are using C#, you can reference the Microsoft.Visua lBasic.dll
          assembly to use the first function.

          Hope this helps
          Jay

          "Henry Chen" <henry.chen@pos tiive-it.com.au> wrote in message
          news:eIJSmUIuDH A.2308@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Hi,
          >
          > I have a string that needs to be parsed into the string[]. The separator[/color]
          is[color=blue]
          > not char[]. It is something like " at ". With current string.Split[/color]
          function,[color=blue]
          > it doesn't work. Is there any exist functions like "Split" that I can use[/color]
          to[color=blue]
          > do the job? Any idea of the simpliest way to do the job?
          >
          > Thanks in advance,
          > Henry
          >
          >[/color]


          Comment

          Working...