I’m relatively new to C#, and am working through a problem whereby I need to strip out all the non-alphanumeric characters within a string.
string thestr = "Hello, my name is Adam";
thestr = thestr.ToLower();
for (int i = 0; i<thestr.Length; i++)
{
if (Char.IsPunctuation(thestr, i) == true||Char.IsWhiteSpace(thestr, i) == true)
{thestr = thestr.Remove(i, 1);}
}
Console.WriteLine(thestr);
Currently it generates this – “hello mynameisadam”
The above is the code I was using, and my logic was it would loop through each character within thestr and then if that char was either punctuation or white space, it would remove it.
That clearly doesn’t happen for the whole string, and I’m willing to put money on it being a relatively simple fix, but I can’t see what I’ve missed to have it do it for part of the string.
user26616360 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.