Trim not working

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm just trying to strip non numeric characters out of a string (phone
number) and the following snippet isn't working. It's pretty straight
forward and should working according to:
http://msdn.microsoft.com/library/d...ref/html/frlrfSystemStringClassTrimTopic2.asp


public string StripAplhanumeric(string sStrip)

{

string sStripLine =
@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*(){}[]',<.>_-/?=+`~
";

string sRslt = sStrip.Trim((sStripLine.ToCharArray())) ;

return sRslt;

}
 
I'm just trying to strip non numeric characters out of a string (phone
number) and the following snippet isn't working. It's pretty straight
forward and should working according to:
http://msdn.microsoft.com/library/d...ref/html/frlrfSystemStringClassTrimTopic2.asp


public string StripAplhanumeric(string sStrip)

{

string sStripLine =
@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*(){}[]',<.>_-/?=+`~
";
string sRslt = sStrip.Trim((sStripLine.ToCharArray())) ;
return sRslt;
}

That won't remove non-numeric characters out of the whole string - just
from each end. From the docs:

<quote>
Removes all occurrences of a set of characters specified in an array
from the beginning and end of this instance.
</quote>

In other words, if the string were "hello5there6bob" the results of
your trim would be "5there6". (You also wouldn't get any trimming for
non-ASCII characters.)

I'd suggest either using a regular expression to replace every
non-numeric character with an empty string, or just creating a
StringBuilder and iterate through the string, appending every numeric
character to the StringBuilder and ignoring the rest.

Jon
 
Got this to work.
public string StripAplhanumeric(string sStrip)

{

string[] saStrip =
sStrip.Split(@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*(){}[]',<.>_-/?=+`~
".ToCharArray(),System.StringSplitOptions.None);

string rslt = string.Join("",saStrip);

return rslt;

}



Too lazy to test the timing. Seems to go pretty quick though.


Jon Skeet said:
I'm just trying to strip non numeric characters out of a string (phone
number) and the following snippet isn't working. It's pretty straight
forward and should working according to:
http://msdn.microsoft.com/library/d...ref/html/frlrfSystemStringClassTrimTopic2.asp


public string StripAplhanumeric(string sStrip)

{

string sStripLine =
@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*(){}[]',<.>_-/?=+`~
";
string sRslt = sStrip.Trim((sStripLine.ToCharArray())) ;
return sRslt;
}

That won't remove non-numeric characters out of the whole string - just
from each end. From the docs:

<quote>
Removes all occurrences of a set of characters specified in an array
from the beginning and end of this instance.
</quote>

In other words, if the string were "hello5there6bob" the results of
your trim would be "5there6". (You also wouldn't get any trimming for
non-ASCII characters.)

I'd suggest either using a regular expression to replace every
non-numeric character with an empty string, or just creating a
StringBuilder and iterate through the string, appending every numeric
character to the StringBuilder and ignoring the rest.

Jon
 
Got this to work.
public string StripAplhanumeric(string sStrip)

{
string[] saStrip =
sStrip.Split(@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*(){}[]',<.>_-/?=+`~
".ToCharArray(),System.StringSplitOptions.None);
string rslt = string.Join("",saStrip);
return rslt;
}


Too lazy to test the timing. Seems to go pretty quick though.

Well, it'll be quick enough that you won't see any visible lag, but it
*is* pretty horribly inefficient (as well as still not stripping
accented characters etc). It may well not be an issue for you, but if
you're going to do millions of these, you might want to go with my
StringBuilder suggestion. I'm on a Mac at the minute so can't test it,
but the code would be something like:

// Note change of method name - "StripAlphaNumeric"
// doesn't describe what
// your method does accurately.
public string StripNonNumeric (string original)
{
StringBuilder builder = new StringBuilder();
foreach (char c in original)
{
if (char.IsDigit (c))
{
builder.Append (c);
}
}
return builder.ToString();
}

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top