String Array Manipulation Problem

Discussion in 'ASP .Net' started by Garfield, Aug 27, 2003.

  1. Garfield

    Garfield Guest

    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,trainee[0].Length - Trainee[0].IndexOf(',') - 1);
    ListItem.Value=Trainee[0].substring(Trainee[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
    Garfield, Aug 27, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message news:...
    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,trainee[0].Length - Trainee[0].IndexOf(',') - 1);
    ListItem.Value=Trainee[0].substring(Trainee[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, Aug 27, 2003
    #2
    1. Advertising

  3. Garfield

    Jon Skeet Guest

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

    Garfield <> wrote:
    > 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(Trainee[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.


    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.IndexOf(',');
    ListItem.Text = nameAndId.Substring (0, commaIndex);
    ListItem.Value = nameAndId.Substring (commaIndex+1);

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet/
    If replying to the group, please do not mail me too
    Jon Skeet, Aug 27, 2003
    #3
  4. 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.Substring(0, Tokens.IndexOf(",")).Trim();
    txtToken2.Text = Tokens.Substring(Tokens.IndexOf(",") +1).Trim();

    Hope this helps,

    - Joe Geretz -

    "Garfield" <> wrote in message
    news:...
    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,trainee[0].Length -
    Trainee[0].IndexOf(',') - 1);
    ListItem.Value=Trainee[0].substring(Trainee[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
    Joseph Geretz, Aug 27, 2003
    #4
  5. Hi Garfield,

    > 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"


    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 -

    > The actual code looks like, it's ina bit of a mess due to formatting,
    > TraineesOnCourse is declared as "string [] TraineesOnCourse" :-
    >
    > ESWebServices.ESWebServices ESW = new
    > Eschedule.ESWebServices.ESWebServices();
    > TraineesOnCourse = ESW.GetTraineesForSchedule((int)theRow["ScheduleID"]);
    > for (int z = 0;z < TraineesOnCourse.Length;z++)
    > {
    > TableCell sTc2 = new TableCell(); // small table row
    > System.Web.UI.WebControls.HyperLink thLink = new HyperLink();
    >
    > // string [] t = TraineesOnCourse[z].Split(',');
    > // thLink.Text = " - " + t[0].ToString() + "<BR>";
    > // thLink.NavigateUrl = "Trainees.aspx?type=show?;traineeID=" +
    > t[1].ToString();
    >
    > string nameAndId = TraineesOnCourse[z];
    > int commaIndex = nameAndId.IndexOf(',');
    > thLink.Text = nameAndId.Substring (0, commaIndex);
    > thLink.NavigateUrl = "Trainees.aspx?type=show?;traineeID=" +
    > nameAndId.Substring (0, commaIndex);
    >
    > sTc2.Controls.Add(thLink);
    > sTr2.Cells.Add(sTc2);
    > sT.Rows.Add(sTr2);
    > }
    > mTCell.Controls.Add(sT); // Add small table to main Cell
    > mTRow.Controls.Add(mTCell); // add a cell to the row
    >
    > "Garfield" <> wrote in message
    > news:...
    > 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,trainee[0].Length -
    > Trainee[0].IndexOf(',') - 1);
    > ListItem.Value=Trainee[0].substring(Trainee[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
    >
    >
    Joseph Geretz, Aug 27, 2003
    #5
  6. Garfield

    Michael Lang Guest

    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" <> wrote in message
    news:exC$L$...
    > Hi Michael,
    >
    > Yeah, I did ask, didn't I? :)
    >
    > I've rarely found any particular mechanism which is 'all GOOD' or 'all

    BAD'.
    > Invariably almost everything involves a tradeoff. IMHO case sensitivity is

    a
    > feature whose potential for damage outweighs its potential for

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

    that
    > C# has more potential for usefullness, than potential for damage) but if

    I'd
    > had my 'druthers, I'd have preferred this one aspect of C# to be

    implemented
    > differently. (Perhaps even an environmental switch to declare case
    > insensitivity within the scope of a project. But that's a lot to ask, and

    I
    > don't even expect this.)
    Michael Lang, Aug 27, 2003
    #6
    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. Anony!

    String Manipulation Problem

    Anony!, Jul 24, 2004, in forum: Java
    Replies:
    30
    Views:
    1,090
    Roedy Green
    Jul 30, 2004
  2. Replies:
    5
    Views:
    4,572
  3. Replies:
    2
    Views:
    535
    Dieter Lamberty
    Aug 10, 2006
  4. Problem with string manipulation

    , Oct 5, 2006, in forum: C Programming
    Replies:
    14
    Views:
    457
    Keith Thompson
    Oct 6, 2006
  5. Coz
    Replies:
    2
    Views:
    97
Loading...

Share This Page