home
links
tips

code
users
tools
tutorials
projects
web
help
design

mudabone
suletzki
trans48
nutrition reality

MarthaAttire!

code Split


C# code for doing a simple split.  There's probably a simpler way to do this but I haven't discovered it yet. 

          using System.Text.RegularExpressions;

          public string[] split(string sData, string sToken)
          {
               Regex r = new Regex("(" + sToken + ")");
               string[] s = r.Split(sData);

               int iHalf = System.Convert.ToInt16((s.GetUpperBound(0) / 2) + 1);
               string[] res = new string[iHalf];

               int j = 0;
               for (int i=0; i <= s.GetUpperBound(0); i++)
               {
                    if (s[i] != sToken)
                    {
                         res[j] = s[i];
                         j++;
                    }
               }
               return res;
          }


back to code          page by wiseleyb@yahoo.com


Comments:

From GeekDaddy [24.145.159.179] - 2/1/05 1:28 AM

Use Regex.Split() instead of String.Split()

From admin [4.5.93.179] - 7/25/04 5:31 PM

You need a function if you want to split on anything more than one character

From meebey [213.39.173.26] - 7/25/04 5:13 PM

string foo = "hello world, here I am";
string[] splitted = foo.Split(new char[] {' '});

foreach(string part in splitted) {
    Console.WriteLine(part);
}

why do you need that split function? C# can do it for you...



Last Modified 8/20/03 4:50 AM

Hide Tools