Reputation: 317
Alright, so basically, I'm trimming a string then making it lowercase. The lowercase part works perfectly well, but the sentence doesn't get trimmed. Any clue?
var result12 = TrimTheSentence(" John. Doe@ gmaiL . cOm");
//the method is
public static string TrimTheSentence(string givenString)
{
givenString = givenString.Trim();
return givenString.ToLower();
Upvotes: 9
Views: 32652
Reputation: 21
You can use a string extension method to remove white space within a string.
public static string RemoveWhiteSpaces(this string input)
{
return Regex.Replace(input, @"\s+", "");
}
Upvotes: 2
Reputation: 3752
This is what you are looking for, you could shorten your method to just one line:
return givenString.Replace(" ", "").ToLower();
Trim()
removes blank spaces from front and end of the string. It will not remove spaces that are in the string.
Examples:
" Test String".Trim(); //Output: "Test String", it will remove only the leading spaces, but not the space between Test and String.
" Test String ".Trim(); //Output: "Test String", it will remove leading and trailing spaces.
MSDN link: http://msdn.microsoft.com/en-us/library/system.string.trim.aspx
Upvotes: 28
Reputation: 84734
Trim removes spaces from the start/end, not from the entire string. Try:
return givenString.Replace(" ", "");
Upvotes: 12