Substring

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim McGivney

    Substring

    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[i];
    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

    #2
    Re: Substring

    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" <mcgiv1@no-spam.sbcglobal. net> wrote in message
    news:O7EM879dGH A.3348@TK2MSFTN GP03.phx.gbl...[color=blue]
    > 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[i];
    > 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
    > }
    >[/color]


    Comment

    Working...