473,385 Members | 1,587 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,385 software developers and data experts.

Removing Characters other than letters or numbers

I have a string where I want to remove only those characters that are
letters or numbers. Does .NET provide an easy method to do this?

TIA!

Jan 11 '06 #1
4 4206
char.IsLetterOrDigit if what you want, looping through the string e.g.

for (int i=0;i < str.Length;i++)
{
if ( !char.IsLetterOrDigit(str[i]) )
// Code here
}

Jan 11 '06 #2
"Yosh" <yo***@nospam.com> wrote in news:u4vrznsFGHA.3912
@TK2MSFTNGP10.phx.gbl:
I have a string where I want to remove only those characters that are
letters or numbers. Does .NET provide an easy method to do this?

TIA!


Regex.Replace would work best for this... Function prototype is:

[C#]
public static string Replace (
string input,
string pattern,
string replacement
)

Example:

string newString;
newString = Regex.Replace(inputString, "[a-zA-Z0-9]", string.Empty);

-mdb
Jan 11 '06 #3
Try using a Regular Expression. That's the easiest way.

public string StripLettersAndNumbers(string input)
{
System.Text.RegularExpressions.Regex regex = new
System.Text.RegularExpressions.Regex("[A-Z0-9]",
System.Text.RegularExpressions.RegexOptions.Ignore Case);
return regex.Replace(input, String.Empty);
}

Jan 11 '06 #4
john doe wrote:
char.IsLetterOrDigit if what you want, looping through the string e.g.

for (int i=0;i < str.Length;i++)
{
if ( !char.IsLetterOrDigit(str[i]) )
// Code here
}


Or even nicer:

foreach (char c in str)
{
if (char.IsLetterOrDigit(c))
{
...
}
}

Jon

Jan 11 '06 #5

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

Similar topics

2
by: Neil Zanella | last post by:
The W3C HTML standard sais the following concerning the HTML input element: <quote> size = cdata This attribute tells the user agent the initial width of the control. The width is given in...
7
by: CharlesEF | last post by:
Hi All, I have run into another problem that is eating my lunch. Should be simple but I am having one heck of a time. Please look at this SELECT statement: SELECT FROM States WHERE ] =...
14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
23
by: Peter Row | last post by:
Hi, I am currently working on a VB.NET project that has been going for quite a while. In the past it has been developed with a lot of compatibility VB6 functions and constants, e.g Left(),...
19
by: many_years_after | last post by:
Hi,everyone: Have you any ideas? Say whatever you know about this. thanks.
1
by: Kosmos | last post by:
Since people have been so helpful on this site I thought I'd contribute what little I can...since I am not a programmer but I'm working on an access database for work Anyways I had to remove...
3
by: Julien | last post by:
Hi, I can't seem to find the right regular expression to achieve what I want. I'd like to remove all characters from a string that are not numbers, letters or underscores. For example: ...
8
by: xiaolim | last post by:
i making a simple program to count the different kinds of characters in a text file and then display them out, however i only manage to count the total numbers of characters. #include...
4
by: Ahmed, Shakir | last post by:
I need to remove text string from the list of the numbers mentioned below: 080829-7_A 070529-5_c 080824-7_O 070405_6_p The output will be : 080829-7 070529-5
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.