locked
Split two numbers with delimiter RRS feed

  • Question

  • Hi everybody!

    I hope i need something very simple (this is my first attempt using Regex).
    Problem (by example):
    1232|4545 : should be split into 1232 and 4545 (delimiter char "|"  should be ignored)
    454656      : should be "split" into 454656    

    I already used the following pattern          ([1-9][0-9]*)([|]([1-9][0-9]*))?        to validate if a given string matches which works great, but i face problems to split the string.
    How can i achieve this?

    Thanks in advance!
    Thursday, August 27, 2009 6:49 PM

Answers

  • Since you already have verified the string format with the regex, the string can be split with either string.Split('|');
    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Proposed as answer by JohnGrove Thursday, August 27, 2009 9:25 PM
    • Marked as answer by Savvas Sopiadis Sunday, August 30, 2009 6:36 PM
    Thursday, August 27, 2009 8:36 PM

All replies

  • Since you already have verified the string format with the regex, the string can be split with either string.Split('|');
    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Proposed as answer by JohnGrove Thursday, August 27, 2009 9:25 PM
    • Marked as answer by Savvas Sopiadis Sunday, August 30, 2009 6:36 PM
    Thursday, August 27, 2009 8:36 PM
  • I think no need of Regex here at all.
    Most language's built-in string functions can do such a split.
    For example in VB
    Dim strs() as string
    Dim str as string
    Dim i as integer
    
    strs = split(str,"|")
    for i = 0 to ubound(strs)
        debug.print strs(i)
    next

    if you need the result in one array, you may need an array to store the result
    instead of printing it out.

    VB has a native split function, while other languages may need write str.split("|") like xalnix wrote.
    (as a method of any string virable.)


    www.wonderstudio.cn
    Sunday, August 30, 2009 5:25 AM
  • Thanks everybody for the replies!

    In the meantime i figured out  myself that i have to use string.split(), but was curious if there is another way of doing this! (your posts are showing me that i am on the right path, since the MS platform and  C# are new territory for me)

    Thanks again for your time
    Sunday, August 30, 2009 6:44 PM