Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text e.x like that: Today, is a good day. Tomorrow, will rain!

now I want to remove , . ! and Spaces

and the result to be
TodayisagooddayTomorrowwillrain.
Posted

Hi,

You can use string function Replace to remove these characters:
C#
string str="Today, is a good day. Tomorrow, will rain!";
str = str.Replace(",", "");
str = str.Replace(".", "");
str = str.Replace("!", "");
str = str.Replace(" ", "");
 
Share this answer
 
Comments
h7h7h7 16-Apr-12 6:45am    
Thnx it works ! :)
ProEnggSoft 16-Apr-12 6:48am    
If the solution works, I think you can vote.
ProEnggSoft 16-Apr-12 6:46am    
Good answer. 5!
I think the Replace method of System.Text.RegularExpressions.Regex class can be used for this purpose

C#
string text = "Today, is     a good day. Tomorrow, will rain!";
//Remove punctuation characters and spaces
string textSansPunctuationSpace = System.Text.RegularExpressions.Regex.Replace(text,@"\p{P}|\s+","", RegexOptions.CultureInvariant);
Console.WriteLine (replacedText);
//Output:
//TodayisagooddayTomorrowwillrain
 
Share this answer
 
v3
Comments
h7h7h7 16-Apr-12 6:46am    
This solution and solution 4 work.
Thnx friends !
VJ Reddy 16-Apr-12 6:50am    
You're welcome and thank you for the response.
ProEnggSoft 16-Apr-12 6:48am    
Good answer. 5!
VJ Reddy 16-Apr-12 6:50am    
Thank you.
Mohammad A Rahman 16-Apr-12 7:00am    
Good Solutions :)
Solution 6 is good. It might try following,

C#
static void Main(string[] args)
{
    string str = "Today, is a good day. Tomorrow, will rain!";
    var result = new String(str.Where(ch => !Char.IsPunctuation(ch) && !Char.IsWhiteSpace(ch)).ToArray());
    Console.WriteLine(result);
}


:)
 
Share this answer
 
Comments
VJ Reddy 16-Apr-12 7:03am    
Excellent answer. 5!
Mohammad A Rahman 16-Apr-12 7:10am    
Thank you VJ :)
Check for Replace() method of string class.
 
Share this answer
 
Comments
CRDave1988 16-Apr-12 6:23am    
http://msdn.microsoft.com/en-us/library/czx8s9ts.aspx
h7h7h7 16-Apr-12 6:27am    
to replace with what...?
e.x I want to remove ".".... replace(".", " " ); you think like that.
phil.o 16-Apr-12 6:39am    
To replace with string.Empty, obviously...
 
Share this answer
 
You may either call repeatedly the String.Replace(Char, Char) method or iterating on string characters generating a new string containing only the wanted ones.

You may also follow this[^] approach.
 
Share this answer
 
Try this code.

C#
public string RemoveChars(string str)
    {
        string[] chars = new string[]{",",".","/","!","@","#","$","%","^","&","*","'","\"",";","-","_","(",")",":","|","[","]"};
        for(int i = 0; i< chars.Length; i++ )
        {
            if(str.Contains(chars[i]))
            {
                str = str.Replace(chars[i],"");
            }
        }
        return str;
    }
 
Share this answer
 

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