Delimiter Split

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

    #1

    Delimiter Split

    Hello,

    The string.Split method is very useful for splitting
    strings that use a single character as a delimiter. i.e.:

    string str = "First;Second;T hird";

    string[] strArray = str.Split(new char[] {';'});

    But I am having trouble getting the Split method to split
    a string with a delimiter of more than one character.
    What is the correct syntax?

    string str = "First<;>Second <;>Third";

    // This does not work, it gives an "Unhandled Exception
    // of type 'System.Executi onEngineExcepti on' in
    // mscorlib.dll"
    string[] strArray = str.Split(new char[] {'<',';','>'});

    Thanks!
  • Natty Gur

    #2
    Re: Delimiter Split

    Hi,

    the relationship between chars is OR and not AND.
    http://msdn.microsoft.com/library/de.../en-us/cpref/h
    tml/frlrfsystemstri ngclasssplittop ic1.asp



    Natty Gur, CTO
    Dao2Com Ltd.
    34th Elkalay st. Raanana
    Israel , 43000
    Phone Numbers:
    Office: +972-(0)9-7740261
    Fax: +972-(0)9-7740261
    Mobile: +972-(0)58-888377


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!

    Comment

    • Chris R. Timmons

      #3
      Re: Delimiter Split

      "Mark Fox" <info@solelsoft ware.com> wrote in
      news:026901c35f 98$5abb7c10$a10 1280a@phx.gbl:
      [color=blue]
      > Hello,
      >
      > The string.Split method is very useful for splitting
      > strings that use a single character as a delimiter. i.e.:
      >
      > string str = "First;Second;T hird";
      >
      > string[] strArray = str.Split(new char[] {';'});
      >
      > But I am having trouble getting the Split method to split
      > a string with a delimiter of more than one character.
      > What is the correct syntax?
      >
      > string str = "First<;>Second <;>Third";
      >
      > // This does not work, it gives an "Unhandled Exception
      > // of type 'System.Executi onEngineExcepti on' in
      > // mscorlib.dll"
      > string[] strArray = str.Split(new char[] {'<',';','>'});[/color]

      Mark,

      To split on multiple characters, use the Regex.Split method:

      using System.Text.Reg ularExpressions ;

      ...

      string str = "First<;>Second <;>Third";
      string[] strArray = Regex.Split(str , "<;>");


      Hope this helps.

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

      Comment

      Working...