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.
for example
"12ab34", "123a5bc", "a123c4" is ok, but "23#45", 2a,35" is not ok.
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.
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.
How can I use Regular Expressions with this question?
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 );
}
Ignacio said:Hi,
String.IndexOfAny is good enough for his requirements. Regex are more
costly to use.
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
static private bool IsValidString( string s )
{
Regex re = new Regex( "^[0-9a-zA-Z]*$" );
return re.IsMatch( s );
}
I assume that by "no symbol" you mean "nothing other than letters orI 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.
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]*$" );
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...
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.