Tuesday, January 25, 2011

How to split a string in CSharp.Net?

Here is the example to split a string with character separator using C#.Net.

string Date = "Feb/24/1981";
if (!string.IsNullOrEmpty(Date))
    char[] separator = new char[] { '/' };
    string[] s1 = Date.Split(separator);
    string Month = s1[0].ToString();
    string Day= s1[1].ToString();
    string Year= s1[2].ToString();

Here is the code to split a string from a string.
string strMain="my name is fewlines4biju and I have make my own website";string[] result = strMain.Split(new string[] { "fewlines4biju" }, StringSplitOptions.None);Then you can retrieve the value on the basis of the Array Index like result[0],result[1] etc.