Sunday, January 2, 2011

C# Split Method

Here is the simple Code for split in C#

// string seperated by colons ';'
string info = "mark;smith;123 csharp drive;toronto;canada";

string[] arInfo = new string[4];

// define which character is seperating fields
char[] splitter = {';'};

arInfo = info.Split(splitter);

for(int x = 0; x < arInfo.Length; x++)
{
Response.Write(arInfo[x] + "<br>");
}


The below also does the same task but we using regulat Expression namespace

using System;
using System.Text.RegularExpressions;

class Program
{
static void Main()
{
string value = "abc\r\nffff\r\ntesting\r\nwelcome";
//
// Split the string on line breaks.
// ... The return value from Split is a string[] array.
//
string[] lines = Regex.Split(value, "\r\n");

foreach (string line in lines)
{
Console.WriteLine(line);
}
}
}

No comments:

Post a Comment