473,748 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remove all non-letter characters from string

hi
I have a string from which I would like to purge all whitespaces,
non-english letters and special characters. I know I can use
String.TrimStar t(char[]) and then provide a list of all unwanted
characters, but the list will have to be large and cumbersome to
maintain. Is there a way to specify only_keep("A-z" && "0-9") or
something similar?

Thanks in advance

Nicki Carstensen
remove the car
Nov 16 '05 #1
3 34842
Nicki Carstensen <ni***@FORDbigf oot.com> wrote:
I have a string from which I would like to purge all whitespaces,
non-english letters and special characters. I know I can use
String.TrimStar t(char[]) and then provide a list of all unwanted
characters, but the list will have to be large and cumbersome to
maintain. Is there a way to specify only_keep("A-z" && "0-9") or
something similar?


Regex.Replace will probably do what you want, or you could fairly
easily hand-code a routine to do what you want. Bear in mind, however,
that "letters" contains much more than A-Z for the international
community - make sure you know exactly what you want to achieve before
you start.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi,

It could be as simple as this: ( not tested )

StringBuilder sb = new StringBuilder ();
for( int i=0; i< string.Length; i++ )
if ( (string[i]>='0' and string[i]<='9' ) || (string[i]>='A' and
string[i]<='z' ) )
sb.Append( string[i] );
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Nicki Carstensen" <ni***@FORDbigf oot.com> wrote in message
news:uH******** ******@TK2MSFTN GP10.phx.gbl...
hi
I have a string from which I would like to purge all whitespaces,
non-english letters and special characters. I know I can use
String.TrimStar t(char[]) and then provide a list of all unwanted
characters, but the list will have to be large and cumbersome to
maintain. Is there a way to specify only_keep("A-z" && "0-9") or
something similar?

Thanks in advance

Nicki Carstensen
remove the car

Nov 16 '05 #3

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:Og******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

It could be as simple as this: ( not tested )

StringBuilder sb = new StringBuilder ();
for( int i=0; i< string.Length; i++ )
if ( (string[i]>='0' and string[i]<='9' ) || (string[i]>='A' and
string[i]<='z' ) )
sb.Append( string[i] );


The above won't work because there may be special characters (such as [
and ]) in between A and z. Moreover, if you want the code to work for
non-English languages too, better use:

StringBuilder sb = new StringBuilder ();
for (int i=0; i< string.Length; i++)
if (char.IsLetterO rDigit(string[i])
sb.Append(strin g[i]);

Regards,
Sami
Nov 16 '05 #4

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

Similar topics

9
37002
by: Ken | last post by:
I am trying to create one image using JavaScript; then later in the script remove the image - not just remove the src. The following creates the image, but I have been unable to remove it. How do I structure document.getElementById('num1').removeChild(image_display); ? <input type=file name="picture1" onChange="image(this.value, 'num1');" Id="pt1"> <div id='num1'></div>
12
5226
by: Oberon | last post by:
I have a large HTML document. It has hundreds of <span>s which have no attributes so these <span>s are redundant. How can I remove these tags automatically? The document also has <span>s with style attributes that I don't want to remove.
3
1718
by: Phil Powell | last post by:
URL: http://www3.brinskter.com/soa/soa_samp.asp?refURL=valsignalandet This URL produces a nice page with form material to produce user-inputted band search information Problem is that Brinkster interferes with the HTML resultset by embedding their annoying ads all over the place. How on earth do I remove them??
2
1896
by: ElGordo | last post by:
I was recently given .Net project files to work on. When I open the Solution file in VS.Net, I always get the message: "The solution appears to be under source control, but its binding information cannot be found. It is possible that the MSSCCPRJ.SCC file or another item that holds the source control settings for the solution, has been deleted. Because it is not possible to recover this missing information automatically, the projects...
6
6678
by: Henry Combrinck | last post by:
Hello all I've been approached by the development people about removing the 'public' schema. They complain about having to manually remove the 'public_' tag from table names generated by their development software whenever they link to PG via ODBC. Renaming or using another schema is not what they're after either. 1. If it is possible to remove the public schema, what are the
80
7881
by: Andrew R | last post by:
Hi I'm creating a series of forms, each with with around 15-20 text boxes. The text boxes will show data from tables, but are unbound to make them more flexible. I want the form to be used for both adding new data and modifying existing data. I have created a save button on the form. When the user clicks the save button, the code checks to see if there
2
1229
by: Jarry | last post by:
I was editing a huge XML file that a friend of mine had made and I didn't need some of the tags. WHen I was finished editing it, I realised I had a lot of information between tags, in no man's land. eg: <team> <player> <name>David Beckham</nameREAL MADRID <positionRight Midfield </position> </player> </team>
3
2719
by: Mark Poppers | last post by:
Assume the following sequence of user actions starting with a Form and an e.g. StatusStrip: 1.) User doubleclicks on the StatusStrip 2.) VisualStudio jumps to the new generated source code mystatusstrip_itemClicked() { ..... } 3.) The user deletes this function from source code (because he decide not to add such an event) 4.) "Rebuild of source code" 5.) An error appears:
33
3725
by: llothar | last post by:
I'm afraid that the GIL is killing the usefullness of python for some types of applications now where 4,8 oder 64 threads on a chip are here or comming soon. What is the status about that for the future of python? I know that at the moment allmost nobody in the scripting world has solved this problem, but it bites and it bites hard. Only groovy as a Java Plugin has support but i never tried it. Writing an interpreter that does MT this...
3
5275
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first add the pane the remove event does not get handled, though thereafter it is handled without problems. ==================================================
0
8995
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9558
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9253
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6798
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6077
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3316
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
2
2791
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.