Trim Space in between the words

Posted by Lakhangarg under C# category on | Views : 3897
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());


/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]

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

Login to post response