Parsing Problem....

Discussion in 'ASP .Net' started by Wallace, Apr 25, 2006.

  1. Wallace

    Wallace Guest

    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....
    Wallace, Apr 25, 2006
    #1
    1. Advertising

  2. 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

    --
    http://www.openmymind.net/



    "Wallace" <> wrote in message
    news:...
    > 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....
    >
    Karl Seguin [MVP], Apr 25, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    874
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    579
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    610
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    435
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    730
    John Levine
    Feb 2, 2012
Loading...

Share This Page