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

How determinate if there no symbol in a string.

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.
Dec 15 '06 #1
13 1645
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
Dec 15 '06 #2
ad
How can I use Regular Expressions with this question?

"Flo 'Irian' Schaetz" <ir***@gmx.de>
???????:45***********************@newsspool1.arcor-online.net...
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

Dec 15 '06 #3


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
Dec 15 '06 #4
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
Dec 15 '06 #5
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.
Dec 15 '06 #6
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" <fl****@wfes.tcc.edu.twwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
How can I use Regular Expressions with this question?

"Flo 'Irian' Schaetz" <ir***@gmx.de>
???????:45***********************@newsspool1.arcor-online.net...
>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


Dec 15 '06 #7
Hi,

String.IndexOfAny is good enough for his requirements. Regex are more
costly to use.
--
Ignacio Machin
machin AT laceupsolutions com

"David Boucherie & Co" <da*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>

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

Dec 15 '06 #8


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. :)
Dec 15 '06 #9
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
Dec 16 '06 #10
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
Dec 16 '06 #11
On Sat, 16 Dec 2006 05:14:53 +0800, "ad" <fl****@wfes.tcc.edu.tw>
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.
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

Dec 16 '06 #12


Flo 'Irian' Schaetz wrote:
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... :)
Dec 16 '06 #13
Flo 'Irian' Schaetz <ir***@gmx.dewrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 16 '06 #14

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

Similar topics

2
by: ad | last post by:
The user of my asp.net program use Sql2005 or Sql2005 express as database, both are possible. I have try both version of data base, I found: If I use Sql2005 express, the Data Source in...
6
by: Xero | last post by:
How do you determinate whether a varible of data type 'Char' is null or not? I used the following code, but it seemed that it didn't work: Dim ans As Char If ans = "" Then MsgBox("Ans...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
2
by: ad | last post by:
How can I determinate if a string is a Valid date. For example "2002/06/23", and "2002/6/23" all are valid date string, but "2002/23/06" is not.
1
by: ad | last post by:
I want to determinate if a value in a Text is digital or character. How can I determinate it with Javascript
7
by: ad | last post by:
I want to determinate if there are alphabets in a string. (not all number) How can I do?
2
by: ad | last post by:
I want to determinate if there are 2 alphabets in a string at least "12ab34", "123a5bc", "a123c4" is ok, but "2345", 2a35" is not ok.
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.