Substring

Discussion in 'ASP .Net' started by Jim McGivney, May 15, 2006.

  1. Jim McGivney

    Jim McGivney Guest

    In an aspx (asp2.0) page in C# I have a string array. The array contains
    letters separated into two groups by a dash (-). I want to obtain the
    letters before the dash. I use the following code. Strangely, when the
    array has a value of "MOD-AS" Label3 displays "3" while Label4 displays -1.
    What's going on ??? What is changing the value of variable mn ? Thanks,
    Jim

    int i = 32;
    string s1;
    string s2;
    s1 = Ante_Arr;
    int mn = s1.IndexOf("-");
    Label3.Text = mn.ToString(); // the displayed value is 3
    try
    {
    s2 = s1.Substring(1, mn); // throws an error: length can not be less
    than zero
    }
    catch
    {
    Label4.Text = mn.ToString(); // the displayed value is -1
    }
     
    Jim McGivney, May 15, 2006
    #1
    1. Advertising

  2. Jim McGivney

    Andrew Hayes Guest

    Maybe it's scope?

    Put the int mn = s1.IndexOf("-"); line into the try block, or take a look at
    the value of mn before the s2 = s1.Substring(1, mn); line.

    "Jim McGivney" <> wrote in message
    news:...
    > In an aspx (asp2.0) page in C# I have a string array. The array contains
    > letters separated into two groups by a dash (-). I want to obtain the
    > letters before the dash. I use the following code. Strangely, when the
    > array has a value of "MOD-AS" Label3 displays "3" while Label4
    > displays -1.
    > What's going on ??? What is changing the value of variable mn ? Thanks,
    > Jim
    >
    > int i = 32;
    > string s1;
    > string s2;
    > s1 = Ante_Arr;
    > int mn = s1.IndexOf("-");
    > Label3.Text = mn.ToString(); // the displayed value is 3
    > try
    > {
    > s2 = s1.Substring(1, mn); // throws an error: length can not be less
    > than zero
    > }
    > catch
    > {
    > Label4.Text = mn.ToString(); // the displayed value is -1
    > }
    >
     
    Andrew Hayes, May 15, 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. =?Utf-8?B?Sm9u?=

    web sddress substring

    =?Utf-8?B?Sm9u?=, Nov 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    393
    Karl Seguin
    Nov 30, 2004
  2. juli
    Replies:
    3
    Views:
    8,909
    Shiva
    Dec 6, 2004
  3. Badass Scotsman

    Finding a SubString within a String

    Badass Scotsman, Mar 31, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    6,155
    S. Justin Gengo
    Mar 31, 2006
  4. Sean
    Replies:
    0
    Views:
    357
  5. Replies:
    3
    Views:
    231
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page