473,574 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 StripAplhanumer ic(string sStrip)

{

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

string sRslt = sStrip.Trim((sS tripLine.ToChar Array())) ;

return sRslt;

}
Jan 23 '06 #1
3 7626
<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 StripAplhanumer ic(string sStrip)

{

string sStripLine =
@"ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz!@#$%^ &*(){}[]',<.>_-/?=+`~
";
string sRslt = sStrip.Trim((sS tripLine.ToChar Array())) ;
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 "hello5there6bo b" 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 StripAplhanumer ic(string sStrip)

{

string[] saStrip =
sStrip.Split(@" ABCDEFGHIJKLMNO PQRSTUVWXYZabcd efghijklmnopqrs tuvwxyz!@#$%^&* (){}[]',<.>_-/?=+`~
".ToCharArray() ,System.StringS plitOptions.Non e);

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.co m> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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 StripAplhanumer ic(string sStrip)

{

string sStripLine =
@"ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz!@#$%^ &*(){}[]',<.>_-/?=+`~
";
string sRslt = sStrip.Trim((sS tripLine.ToChar Array())) ;
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 "hello5there6bo b" 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 StripAplhanumer ic(string sStrip)

{
string[] saStrip =
sStrip.Split(@" ABCDEFGHIJKLMNO PQRSTUVWXYZabcd efghijklmnopqrs tuvwxyz!@#$%^&* (){}[]',<.>_-/?=+`~
".ToCharArray() ,System.StringS plitOptions.Non e);
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 - "StripAlphaNume ric"
// 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.ToStrin g();
}

Jon

Jan 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4093
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 database, trailing and leading spaces are removed using a call to the Trim()function. This works the first time data is inserted into the database....
22
12733
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' or even why there could be a problem. here is the code ////////
5
10524
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. I had a number of textboxes in the report designs that used the trim() function to suppress printing of leading spaces. As an example, one of the...
3
10683
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 fields: BuildName and RoadName. Both have the following properties: size 50, not indexed, not required, allowed zero length. Some records have...
9
6268
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 can i make a fast Right Trim Function in c,using Binary search kind of fast algorithm ? Offcourse...I can use the classical approach too. like :...
11
5337
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 (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
5
15003
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
9725
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 thought I'd change it to the VB ..NET method "String.Trim" but that throws an object exception. Which brings the question: is it compliant to use...
121
5047
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
7835
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7753
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8095
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8265
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7847
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5645
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5332
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2265
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1365
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.