471,319 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Trim not working

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/de...TrimTopic2.asp
public string StripAplhanumeric(string sStrip)

{

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

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

return sRslt;

}
Jan 23 '06 #1
3 7535
<drch...@nospam.nospam> wrote:
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/de...TrimTopic2.asp
public string StripAplhanumeric(string sStrip)

{

string sStripLine =
@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz!@#$%^&*(){}[]',<.>_-/?=+`~
";
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

Jan 23 '06 #2
Got this to work.
public string StripAplhanumeric(string sStrip)

{

string[] saStrip =
sStrip.Split(@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi jklmnopqrstuvwxyz!@#$%^&*(){}[]',<.>_-/?=+`~
".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 [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
<drch...@nospam.nospam> wrote:
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/de...TrimTopic2.asp
public string StripAplhanumeric(string sStrip)

{

string sStripLine =
@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz!@#$%^&*(){}[]',<.>_-/?=+`~
";
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

Jan 24 '06 #3
dr*****@nospam.nospam wrote:
Got this to work.
public string StripAplhanumeric(string sStrip)

{
string[] saStrip =
sStrip.Split(@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi jklmnopqrstuvwxyz!@#$%^&*(){}[]',<.>_-/?=+`~
".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

Jan 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dominic | last post: by
22 posts views Thread by Simon | last post: by
5 posts views Thread by Dat K. AU DUONG | last post: by
22 posts views Thread by Terry Olsen | last post: by
121 posts views Thread by swengineer001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.