473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 7617
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dominic | last post by:
Here is the general problem: Information is taken from a form (consisting of text boxes, drop down lists et cetera) and submitted to an Access database. Before the data is inserted into the...
22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
5
by: Dave Bovey | last post by:
I created a church database in Access 97 that contain the typical Name, Address, and Phone Number information fields. I had a number of different reports to print query data in different formats. ...
3
by: Andy B | last post by:
I've tried using Trim or RTrim to strip trailing space characters from my data. When I check on the transformed data space characters are still there. We have an address table containing two...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
5
by: Dat K. AU DUONG | last post by:
Hi, C# Annoying string function or Am I doing Something wrong? In VB6/VB.NET I can code like: value = value.trim() and it just work!
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
121
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.