Click here to Skip to main content
15,884,849 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hi All,

I have a string of numbers with some extra symbols like 123!2@2344#242%^. The symbols could be anywhere between the numbers. I should remove all those symbols.

I used
XML
char[] MyChar = {'-', '_', '(', ')', '{', '}', '[', ']', '<', '>', '@', '#', '$', '&', '*', '\'', ':', ';', ',' };
string strNumbers = Numbers.TrimEnd(MyChar);

but not working

Thanks in advance
Posted

What about this?

C#
string input = "123!2@2344#242%^.";
string result = new string(input.Where(c => char.IsDigit(c)).ToArray());
 
Share this answer
 
Comments
idhris2011 18-Nov-14 6:27am    
Thanks Manas. That works
Manas Bhardwaj 18-Nov-14 6:29am    
Glad it works. I would suggest that you accept this solution and vote appropriately, so that it can help other people as well.
Maciej Los 18-Nov-14 7:12am    
5ed!
Linq rocks!
Manas Bhardwaj 18-Nov-14 7:18am    
Thanks. Yes, it's very handy sometimes!

Takes sometime to get used to it though.
BillWoodruff 18-Nov-14 8:35am    
+5
"Not working" is not informative at all...

Test it:
C#
char[] MyChar = {'^', '%', '!', '-', '_', '(', ')', '{', '}', '[', ']', '<', '>', '@', '#', '$', '&', '*', '\'', ':', ';', ',' };
string strNumbers = @"123!2@2344#242%^";

string[] MyNumbers = strNumbers.Split(MyChar);
foreach(string num in MyNumbers)
{
    Console.WriteLine(num);
}


Result:
123
2
2344
242
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Nov-14 9:58am    
5ed. I would only add: one cannot remove anything from a string, which is a read-only object. A string function can only create a brand-new string.
—SA
Maciej Los 18-Nov-14 13:07pm    
Thank you, Sergey ;)

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