Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
why this trim function is not working..

C#
string str = content.trim(new char[] {',', '.', ';'})


i want to remove all the commas, full stops and semi-colons from my string, but this function is not working....
Posted

Trim will remove character from start or end. Not in between characters are removed by Trim function.
You can use Replace or Remove functions or Reg Ex expression.

C#
Regex pattern = new Regex("[;,.]");
string str = pattern.Replace(content, "");
 
Share this answer
 
v3
Trim only works on the left and right ends of the string, it doesn't do anything about the middle!

Try a regex:
C#
string without = Regex.Replace(inputString, @"[,\.;]+", "");
 
Share this answer
 
You are supposed to use Replace() method instead of Trim()

Trim eliminates leading and trailing whitespace. We need to remove whitespace from the beginning or ending of a string. We use the .NET Framework's Trim method to do this efficiently. This method removes any characters specified.

C#
string input = "Hi,I'm a sentence having special characters.;";

input = input.Replace("'", "").Replace(",", "").Replace(";", "").Replace(".", "");
 
Share this answer
 
Comments
ShobuGpta 26-Jun-14 7:20am    
this was much easier thanks
Prasad Avunoori 26-Jun-14 7:59am    
But, Solution1 by OriginalGriff was better as it used less code than me.
OriginalGriff 26-Jun-14 15:13pm    
More to the point, it generates fewer interim strings - which can be significant both in memory and time when the input gets large.
ShobuGpta 26-Jun-14 8:03am    
yeah, but i never understand these Regex patterns, too complicated
OriginalGriff 26-Jun-14 15:14pm    
Learn them! They aren't really difficult, and they do save a heck of a lot of time.
Get a copy of Expresso
http://www.ultrapico.com/Expresso.htm
It's free, and it examines and generates Regular expressions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900