Parsing Problem....

W

Wallace

Hai all,

In VC++,

CString str = " 4 76 1";
int nNo = atoi(str.Mid(0,8));

it returns nNo = 4 as output.... it returns the first integer
occurence and neglects the remaining character....


Similarly, In C#.NET I do the same as following....

string str = " 4 76 1";
int nNo = int.Parse(str.Substring(0, 8));

it is throwing the exception as "Input string is not in correct
format....."

How can i get the output as 4(i.e nNo = 4) in c#.net ????
Is there any other function other than parse, to fulfil this??

Please help... Thanx in advance...
Urgent..... Looking forward for the ur response....
 
K

Karl Seguin [MVP]

Well, if the # is always in the format of " # ## #" then you can simply do
something like:

str.Substring(1,1);

if the # format is a little different, but space delimited, I'd split the
string on a space

string[] numbers = str.Split(' ');
numbers[1]; (it's 1 because of the leading space). You could also just
Trim() the str to get rid of those.

If your pattern is any more complexe, I'd recommend using a regular
expression.

Karl
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top