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

How to check a string

ad
How to check a string if this string is composite of number?
Nov 17 '05 #1
6 1436
Hi ad,

What exactly did you mean by "composite of number"?

Could you please describe in more detail what you are trying to accomplish?

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.msmvps.com/rakeshrajan/

"ad" wrote:
How to check a string if this string is composite of number?

Nov 17 '05 #2
ad

It must be composite of char 0..9
Thank

"Rakesh Rajan" <rakeshrajan {at} mvps {dot} org> ¼¶¼g©ó¶l¥ó·s»D:2C********************************* *@microsoft.com...
Hi ad,

What exactly did you mean by "composite of number"?

Could you please describe in more detail what you are trying to
accomplish?

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.msmvps.com/rakeshrajan/

"ad" wrote:
How to check a string if this string is composite of number?

Nov 17 '05 #3
Hi,

You can use:

bool IsInteger(string s)
{
try
{
int n = int.Parse(s);
return true;
}
catch
{
return false;
}

This will accept a '+' or '-' sign in front of a number. If you don't want
that, you could write your own function:

bool IsInteger(string s)
{
for (int i = 0; i < s.Length; i++)
if (s[i] < '0' || s[i] > '9')
return false;
return true;
}

Check against a regular expression could be another solution.

Regards - Octavio

"ad" <ad@wfes.tcc.edu.tw> escribió en el mensaje
news:Ol**************@TK2MSFTNGP14.phx.gbl...
How to check a string if this string is composite of number?

Nov 17 '05 #4
Octavio Hernandez wrote:
Check against a regular expression could be another solution.


Yep, and much easier:

if (Regex.IsMatch(myString, "\d+"))
// myString contains at least one character 0-9
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #5
You're absolutely right, Oliver!

Regards - Octavio

"Oliver Sturm" <ol****@sturmnet.org> escribió en el mensaje
news:e8**************@tk2msftngp13.phx.gbl...
Octavio Hernandez wrote:
Check against a regular expression could be another solution.


Yep, and much easier:

if (Regex.IsMatch(myString, "\d+"))
// myString contains at least one character 0-9
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #6
That was real clever Oliver :)

BTW, I think the regex should be "^\d+$"

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.msmvps.com/rakeshrajan/

"Oliver Sturm" wrote:
Octavio Hernandez wrote:
Check against a regular expression could be another solution.


Yep, and much easier:

if (Regex.IsMatch(myString, "\d+"))
// myString contains at least one character 0-9
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #7

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

Similar topics

27
by: John Roth | last post by:
PEP 263 is marked finished in the PEP index, however I haven't seen the specified Phase 2 in the list of changes for 2.4 which is when I expected it. Did phase 2 get cancelled, or is it just not...
5
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to...
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
2
by: MikeY | last post by:
Hi all, I am coding window forms in C#. My problem is this: I have created a "Check ListView" or a 'ListView' with checkbox's. I have populated the it with my files from my folders, mps, txt,...
5
by: Shapper | last post by:
Hello, I am using this code line to check i a variable exists: If cookie Is Nothing Then .... How can I check if cookie is empty when cookie exists? Thanks, Miguel
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
8
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on...
5
by: Gary Wessle | last post by:
hi or is there a better way to check the existence of "py" in a string "s"? string s = "djrfpyfd"; string t = "py"; string r = s.substr(s.find("py"),2); cout << (t==r) << endl;
5
by: Andrew Meador | last post by:
I have a form (Change Card List by Status) with a check box (cboNOT) and a list box (lstStatus). There is an Open Report button that opens a report (Report - Change Card List) which uses a query...
55
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of...
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...
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.