String Array Manipulation Problem

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

    String Array Manipulation Problem

    Hello

    I have a function that returns a string array. The string has a name and an ID number, they are separated by a comma.
    I cannot for the life of me by using the string methods get to separate the two items.

    Example.

    The string array looks like, the array is called Trainee :-

    Joe Bloggs,5678
    Henry Kissinger,2345

    I want to separate the two using the ',' as the separator because I'm building a drop-down list
    I have tried to get the name part by doing

    ListItem.Text = Trainee[0].substring(0,tr ainee[0].Length - Trainee[0].IndexOf(',') - 1);
    ListItem.Value= Trainee[0].substring(Trai nee[0].IndexOf(',') + 1,4);

    This doesn't compile, the string methods are not being recognised and I can't see how to do it. I mean I could change my function to send back two variables, but I would prefer to use the language to get my results.

    Thanks for any help.
    Regards
    Garfield

  • ozgur develioglu

    #2
    Re: String Array Manipulation Problem

    Hi,

    You may use split() methot of string. It reduces your code size and doesn't depend on the length of your strings.

    string[] x = Trainee[0].Split(',');

    ListItem.Text = x[0];

    ListItem.Value= x[1];

    Hope this help.

    "Garfield" <gboodie@hotmai l.com> wrote in message news:O5hrnXHbDH A.1748@TK2MSFTN GP12.phx.gbl...
    Hello

    I have a function that returns a string array. The string has a name and an ID number, they are separated by a comma.
    I cannot for the life of me by using the string methods get to separate the two items.

    Example.

    The string array looks like, the array is called Trainee :-

    Joe Bloggs,5678
    Henry Kissinger,2345

    I want to separate the two using the ',' as the separator because I'm building a drop-down list
    I have tried to get the name part by doing

    ListItem.Text = Trainee[0].substring(0,tr ainee[0].Length - Trainee[0].IndexOf(',') - 1);
    ListItem.Value= Trainee[0].substring(Trai nee[0].IndexOf(',') + 1,4);

    This doesn't compile, the string methods are not being recognised and I can't see how to do it. I mean I could change my function to send back two variables, but I would prefer to use the language to get my results.

    Thanks for any help.
    Regards
    Garfield

    Comment

    • Jon Skeet

      #3
      Re: String Array Manipulation Problem

      [If you could wrap your posts at about 70 characters, it would make
      them easier to reply to.]

      Garfield <gboodie@hotmai l.com> wrote:[color=blue]
      > I have a function that returns a string array. The string has a name
      > and an ID number, they are separated by a comma.
      > I cannot for the life of me by using the string methods get to separate
      > the two items.
      >
      > Example.
      >
      > The string array looks like, the array is called Trainee :-
      >
      > Joe Bloggs,5678
      > Henry Kissinger,2345
      >
      > I want to separate the two using the ',' as the separator because I'm
      > building a drop-down list. I have tried to get the name part by doing
      >
      > ListItem.Text = Trainee[0].substring
      > (0,trainee[0].Length - Trainee[0].IndexOf(',') - 1);
      > ListItem.Value= Trainee[0].substring(Trai nee[0].IndexOf(',') + 1,4);
      >
      > This doesn't compile, the string methods are not being recognised and I
      > can't see how to do it. I mean I could change my function to send back
      > two variables, but I would prefer to use the language to get my results.[/color]

      Well, Substring isn't part of the language, it's part of the library -
      and the answer to the compilation problem is simple: the method is
      called Substring, not substring. C# is case-sensitive. You're also
      using Trainee in several places and trainee in another - you need to be
      consistent. The "length" part of your first substring looks dodgy too.

      I would seriously consider rewriting that code though. For one thing,
      if there's definitely only a single comma there, use String.Split
      instead, just for simplicity. If you don't want to do that, rewrite the
      above as something like:

      string nameAndId = Trainee[0];
      int commaIndex = nameAndId.Index Of(',');
      ListItem.Text = nameAndId.Subst ring (0, commaIndex);
      ListItem.Value = nameAndId.Subst ring (commaIndex+1);

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Joseph Geretz

        #4
        Re: String Array Manipulation Problem

        Hello Garfield,

        It looks like your using C#? The name of the string function is Substring,
        not substring.

        (All those who are in favor of case sensitivity can now jump on board and
        tell us why this is such an excellent thing. After all, it's useful to be
        able to have different methods named Substring, substring and SubString,
        right? :-( )

        Also, your code sample seemed overly complex. Take a look at the two
        statements below. Using a compond text string (e.g. 'Name,1234') named
        Tokens, the first statement parses out everything prior to the comma, the
        second statment parses out everything after the comma.

        txtToken1.Text = Tokens.Substrin g(0, Tokens.IndexOf( ",")).Trim( );
        txtToken2.Text = Tokens.Substrin g(Tokens.IndexO f(",") +1).Trim();

        Hope this helps,

        - Joe Geretz -

        "Garfield" <gboodie@hotmai l.com> wrote in message
        news:O5hrnXHbDH A.1748@TK2MSFTN GP12.phx.gbl...
        Hello

        I have a function that returns a string array. The string has a name and an
        ID number, they are separated by a comma.
        I cannot for the life of me by using the string methods get to separate the
        two items.

        Example.

        The string array looks like, the array is called Trainee :-

        Joe Bloggs,5678
        Henry Kissinger,2345

        I want to separate the two using the ',' as the separator because I'm
        building a drop-down list
        I have tried to get the name part by doing

        ListItem.Text = Trainee[0].substring(0,tr ainee[0].Length -
        Trainee[0].IndexOf(',') - 1);
        ListItem.Value= Trainee[0].substring(Trai nee[0].IndexOf(',') + 1,4);

        This doesn't compile, the string methods are not being recognised and I
        can't see how to do it. I mean I could change my function to send back two
        variables, but I would prefer to use the language to get my results.

        Thanks for any help.
        Regards
        Garfield


        Comment

        • Joseph Geretz

          #5
          Re: String Array Manipulation Problem

          Hi Garfield,
          [color=blue]
          > I have tried all the suggestions so far to no avail. I get the error
          > message "Object Reference not set to an instance of a object"[/color]

          Thanks for the code, but on which line does the error occur? You're
          evidently trying to work with some object which hasn't been initialized.
          (Remember, strings are also objects.)

          - Joe Geretz -
          [color=blue]
          > The actual code looks like, it's ina bit of a mess due to formatting,
          > TraineesOnCours e is declared as "string [] TraineesOnCours e" :-
          >
          > ESWebServices.E SWebServices ESW = new
          > Eschedule.ESWeb Services.ESWebS ervices();
          > TraineesOnCours e = ESW.GetTrainees ForSchedule((in t)theRow["ScheduleID "]);
          > for (int z = 0;z < TraineesOnCours e.Length;z++)
          > {
          > TableCell sTc2 = new TableCell(); // small table row
          > System.Web.UI.W ebControls.Hype rLink thLink = new HyperLink();
          >
          > // string [] t = TraineesOnCours e[z].Split(',');
          > // thLink.Text = " - " + t[0].ToString() + "<BR>";
          > // thLink.Navigate Url = "Trainees.aspx? type=show?;trai neeID=" +
          > t[1].ToString();
          >
          > string nameAndId = TraineesOnCours e[z];
          > int commaIndex = nameAndId.Index Of(',');
          > thLink.Text = nameAndId.Subst ring (0, commaIndex);
          > thLink.Navigate Url = "Trainees.aspx? type=show?;trai neeID=" +
          > nameAndId.Subst ring (0, commaIndex);
          >
          > sTc2.Controls.A dd(thLink);
          > sTr2.Cells.Add( sTc2);
          > sT.Rows.Add(sTr 2);
          > }
          > mTCell.Controls .Add(sT); // Add small table to main Cell
          > mTRow.Controls. Add(mTCell); // add a cell to the row
          >
          > "Garfield" <gboodie@hotmai l.com> wrote in message
          > news:O5hrnXHbDH A.1748@TK2MSFTN GP12.phx.gbl...
          > Hello
          >
          > I have a function that returns a string array. The string has a name and[/color]
          an[color=blue]
          > ID number, they are separated by a comma.
          > I cannot for the life of me by using the string methods get to separate[/color]
          the[color=blue]
          > two items.
          >
          > Example.
          >
          > The string array looks like, the array is called Trainee :-
          >
          > Joe Bloggs,5678
          > Henry Kissinger,2345
          >
          > I want to separate the two using the ',' as the separator because I'm
          > building a drop-down list
          > I have tried to get the name part by doing
          >
          > ListItem.Text = Trainee[0].substring(0,tr ainee[0].Length -
          > Trainee[0].IndexOf(',') - 1);
          > ListItem.Value= Trainee[0].substring(Trai nee[0].IndexOf(',') + 1,4);
          >
          > This doesn't compile, the string methods are not being recognised and I
          > can't see how to do it. I mean I could change my function to send back[/color]
          two[color=blue]
          > variables, but I would prefer to use the language to get my results.
          >
          > Thanks for any help.
          > Regards
          > Garfield
          >
          >[/color]


          Comment

          • Michael Lang

            #6
            Re: String Array Manipulation Problem

            It seems in life that the more power you have, the more potential for
            disaster. This isn't limited to software development. Take a look at the
            presidency of the USA. Some have used the power well and have accomplished
            alot with it, others do a sloppy job with many bad side effects.

            This is why multiple languages have been developed. There is no cure all
            that works in all situations and for all users.

            --
            Michael Lang, MCSD

            "Joseph Geretz" <jgeretz@nospam .com> wrote in message
            news:exC$L$MbDH A.2580@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Hi Michael,
            >
            > Yeah, I did ask, didn't I? :-)
            >
            > I've rarely found any particular mechanism which is 'all GOOD' or 'all[/color]
            BAD'.[color=blue]
            > Invariably almost everything involves a tradeoff. IMHO case sensitivity is[/color]
            a[color=blue]
            > feature whose potential for damage outweighs its potential for[/color]
            usefullness.[color=blue]
            > That's all I'm trying to say. (Personally, I'd stay away from using case
            > sensitivity to distinguish between my own names which might otherwise
            > conflict with keywords. Naturally, that's just my approach, but it is part
            > of the basis for why I don't see the benefits of case sensitivity as
            > outweighing the drawbacks.)
            >
            > Having been delivered in this manner, it's not the most major deal. It
            > hasn't stopped me fron transitioning to C# (on the whole, I do beleive[/color]
            that[color=blue]
            > C# has more potential for usefullness, than potential for damage) but if[/color]
            I'd[color=blue]
            > had my 'druthers, I'd have preferred this one aspect of C# to be[/color]
            implemented[color=blue]
            > differently. (Perhaps even an environmental switch to declare case
            > insensitivity within the scope of a project. But that's a lot to ask, and[/color]
            I[color=blue]
            > don't even expect this.)[/color]


            Comment

            Working...