Trim Space in between the words

Lakhangarg
Posted by Lakhangarg under C# category on | Views : 3776
Code to remove White Space From the string.

public static string TrimString(string strInput)
{
return TrimStringData(strInput, "");
}

public static string TrimStringData(string strInput, string replaceWith)
{
string strTitle = Regex.Replace(strInput, @"[^\w]", replaceWith);
strTitle = Regex.Replace(strTitle, @"([\-]{1,})", replaceWith);
return strTitle.Trim(replaceWith.ToCharArray());
}


Output:

/Call This Method:
TrimString("Lakhan Pal Garg")
This will return: LakhanPalGarg

Comments or Responses

Posted by: Lakhangarg on: 9/4/2009 Level:Silver | Status: [Member] [Moderator]
Hi-

we can use this too:
string str= "Lakhan Pal Garg";
str = str.Replace(" ", "");


Login to post response