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

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

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:
    1,120
    GIMME
    Feb 11, 2004
  2. Arsalan Ahmad

    Parsing problem

    Arsalan Ahmad, Sep 4, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    15,965
    Rutger Smit
    Sep 4, 2004
  3. =?Utf-8?B?RGFuIENvcmt1bQ==?=

    http form parsing problem...

    =?Utf-8?B?RGFuIENvcmt1bQ==?=, Jan 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    397
    =?Utf-8?B?RGFuIENvcmt1bQ==?=
    Jan 20, 2005
  4. Pumkin

    Parsing a label collection problem

    Pumkin, Feb 3, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    349
    Pumkin
    Feb 3, 2006
  5. Naren
    Replies:
    0
    Views:
    769
    Naren
    May 11, 2004
  6. Christopher Diggins
    Replies:
    0
    Views:
    749
    Christopher Diggins
    Jul 9, 2007
  7. Christopher Diggins
    Replies:
    0
    Views:
    568
    Christopher Diggins
    Jul 9, 2007
  8. John Levine
    Replies:
    0
    Views:
    921
    John Levine
    Feb 2, 2012
Loading...