How determinate if there no symbol in a string.

A

ad

I want to determinate if there no symbol in a string?
for example
"12ab34", "123a5bc", "a123c4" is ok, but "23#45", 2a,35" is not ok.
 
F

Flo 'Irian' Schaetz

And thus, ad spoke...
I want to determinate if there no symbol in a string?
for example
"12ab34", "123a5bc", "a123c4" is ok, but "23#45", 2a,35" is not ok.

You probably want to search for "Regular Expressions". Hope that helps :)

Flo
 
D

David Boucherie & Co

ad said:
I want to determinate if there no symbol in a string?
for example
"12ab34", "123a5bc", "a123c4" is ok, but "23#45", 2a,35" is not ok.


You could use regular expressions...
Here some code demonstrating the IsValidString() function that checks
your requirements for your strings.

-----------------------------------------------

using System.Text.RegularExpressions;

private void button1_Click( object sender, EventArgs e )
{
CheckStrings();
}

static private void CheckStrings()
{
StringBuilder sb = new StringBuilder();
string[] arr = new string[]
{ "12ab34", "123a5bc", "a123c4", "23#45", "2a,35" };
foreach ( string s in arr )
{
sb.Append( s );
sb.Append( " is " );
sb.Append( IsValidString( s ) ? "OK" : "not OK" );
sb.Append( "\n" );
}
MessageBox.Show( sb.ToString() );
}

static private bool IsValidString( string s )
{
Regex re = new Regex( "^[0-9a-zA-Z]*$" );
return re.IsMatch( s );
}
 
F

Flo 'Irian' Schaetz

And thus, ad spoke...
How can I use Regular Expressions with this question?

Google is a really great friend, you know... If you are asking it
nicely, it will show you dozens of pages where you'll find tutorials and
examples on how to use regular expressions in C#... I'm not nearly good
enough to write such fine texts myself, so it would be wise to search
for them instead relying on me :)

Flo
 
R

RvGrah

I'm sure Regular Expressions is probably good advice, but since I'm not
up to speed in that area, I'd tell you to iterate through the string a
character at a time and use "if (!char.isDigit || !char.isLetter)" to
test for a symbols and act on that. (pseudo code used in illustration,
though the isDigit and isLetter booleans are part of .net framework).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You will need to check your string for one of several chars , (do not like
the symbol word)

You do not really need to use regular expression, all you have to do is use

String.IndexOfAny( new char[] { '#', ',' .......... } );

it will return -1 if no char from the array was found
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

String.IndexOfAny is good enough for his requirements. Regex are more
costly to use.


--
Ignacio Machin
machin AT laceupsolutions com

David Boucherie & Co said:
I want to determinate if there no symbol in a string?
for example
"12ab34", "123a5bc", "a123c4" is ok, but "23#45", 2a,35" is not ok.


You could use regular expressions...
Here some code demonstrating the IsValidString() function that checks your
requirements for your strings.

-----------------------------------------------

using System.Text.RegularExpressions;

private void button1_Click( object sender, EventArgs e )
{
CheckStrings();
}

static private void CheckStrings()
{
StringBuilder sb = new StringBuilder();
string[] arr = new string[]
{ "12ab34", "123a5bc", "a123c4", "23#45", "2a,35" };
foreach ( string s in arr )
{
sb.Append( s );
sb.Append( " is " );
sb.Append( IsValidString( s ) ? "OK" : "not OK" );
sb.Append( "\n" );
}
MessageBox.Show( sb.ToString() );
}

static private bool IsValidString( string s )
{
Regex re = new Regex( "^[0-9a-zA-Z]*$" );
return re.IsMatch( s );
}
 
D

David Boucherie & Co

Ignacio said:
Hi,

String.IndexOfAny is good enough for his requirements. Regex are more
costly to use.

Except of course Ad probably doesn't want to list every non-digit and
non-letter symbol in existance in an array.

And what's costly with today's computers? I'm not sure it'll matter too
much, unless he's using this in a zillion iterations loop. :)
 
F

Flo 'Irian' Schaetz

And thus, Ignacio Machin ( .NET/ C# MVP ) spoke...
You do not really need to use regular expression, all you have to do is use

String.IndexOfAny( new char[] { '#', ',' .......... } );

it will return -1 if no char from the array was found

Depends on what he wants to do... If he ONLY wants to allow alphanumeric
characters, I would use a regular expression - because then your char
array would become rather long. If the number of forbidden chars is
limited, I would use your way...

Flo
 
F

Flo 'Irian' Schaetz

And thus, David Boucherie & Co spoke...
static private bool IsValidString( string s )
{
Regex re = new Regex( "^[0-9a-zA-Z]*$" );
return re.IsMatch( s );
}

I'm not sure (in C#): Wouldn't it be faster to write...

private Regex re = new Regex(""^[0-9a-zA-Z]*$" );

static private bool isValidString( string s ) {
return re.isMatch( s );
}

? So you wouldn't construct a new RegEx every time the method is called...

Flo
 
R

rossum

I want to determinate if there no symbol in a string?
for example
"12ab34", "123a5bc", "a123c4" is ok, but "23#45", 2a,35" is not ok.
I assume that by "no symbol" you mean "nothing other than letters or
digits. If this is so then char.IsLetterOrDigit() would seem to be
what you want:


static bool IsValidString(string text) {
foreach (char c in text) {
if (!char.IsLetterOrDigit(c)) { return false; }
}
return true;
}

static void Main() {
string[] strings = { "12ab34", "123a5bc", "a123c4",
"23#45", "2a,35" };

foreach (string str in strings) {
Console.Write("String {0} is ", str);
if (IsValidString(str)) {
Console.WriteLine("good.");
} else {
Console.WriteLine("bad.");
}
}
}


rossum
 
D

David Boucherie & Co

Flo said:
And thus, David Boucherie & Co spoke...
static private bool IsValidString( string s )
{
Regex re = new Regex( "^[0-9a-zA-Z]*$" );
return re.IsMatch( s );
}


I'm not sure (in C#): Wouldn't it be faster to write...
private Regex re = new Regex("^[0-9a-zA-Z]*$" );

It sure would. :)
You are right, of course. So, Ad, what Irian said... :)
 
J

Jon Skeet [C# MVP]

Flo 'Irian' Schaetz said:
And thus, Ignacio Machin ( .NET/ C# MVP ) spoke...
You do not really need to use regular expression, all you have to do is use

String.IndexOfAny( new char[] { '#', ',' .......... } );

it will return -1 if no char from the array was found

Depends on what he wants to do... If he ONLY wants to allow alphanumeric
characters, I would use a regular expression - because then your char
array would become rather long. If the number of forbidden chars is
limited, I would use your way...

Well, using your suggestion of ^[0-9a-zA-Z] knocks out several
potentially valid characters - what about a letter with an accent on?

If I *were* to use a regular expression, I'd probably use the character
classes rather than specific letters. If I didn't want to use regular
expressions, I'd iterate over the characters and use
Char.IsLetterOrDigit etc.

Of course, we don't have a rigorous definition of what the OP really
wants - what counts as a symbol in his terminology.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top