PC Review


Reply
Thread Tools Rate Thread

How determinate if there no symbol in a string.

 
 
ad
Guest
Posts: n/a
 
      15th Dec 2006
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.


 
Reply With Quote
 
 
 
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      15th Dec 2006
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
 
Reply With Quote
 
ad
Guest
Posts: n/a
 
      15th Dec 2006
How can I use Regular Expressions with this question?

"Flo 'Irian' Schaetz" <(E-Mail Removed)>
???????:458311c7$0$30318$(E-Mail Removed)...
> 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



 
Reply With Quote
 
David Boucherie & Co
Guest
Posts: n/a
 
      15th Dec 2006


ad wrote:
> 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 );
}

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

Have fun with it.
David
 
Reply With Quote
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      15th Dec 2006
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
 
Reply With Quote
 
RvGrah
Guest
Posts: n/a
 
      15th Dec 2006
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).

Bob
ad wrote:
> 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.


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      15th Dec 2006
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


--
Ignacio Machin
machin AT laceupsolutions com

"ad" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> How can I use Regular Expressions with this question?
>
> "Flo 'Irian' Schaetz" <(E-Mail Removed)>
> ???????:458311c7$0$30318$(E-Mail Removed)...
>> 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

>
>



 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      15th Dec 2006
Hi,

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


--
Ignacio Machin
machin AT laceupsolutions com

"David Boucherie & Co" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
>
> ad wrote:
>> 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 );
> }
>
> -----------------------------------------------
>
> Have fun with it.
> David



 
Reply With Quote
 
David Boucherie & Co
Guest
Posts: n/a
 
      15th Dec 2006


Ignacio Machin ( .NET/ C# MVP ) wrote:

> 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.
 
Reply With Quote
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      16th Dec 2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string and less then symbol Tony M Microsoft VB .NET 3 7th Aug 2007 11:44 AM
How determinate if there are 2 alphabets in a string at least ad Microsoft C# .NET 2 3rd Dec 2006 07:49 AM
How to determinate if an character in a string ad Microsoft C# .NET 7 3rd Dec 2006 03:30 AM
How to determinate if a string in the Field names of a dataTable? ad Microsoft ADO .NET 1 19th Aug 2006 09:05 AM
How to determinate if a string is a Date ad Microsoft C# .NET 2 16th Jun 2006 02:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:43 PM.