Professional Web Applications Themes

String Array Manipulation Problem - ASP.NET General

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

  1. #1

    Default 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,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 Guest

  2. #2

    Default 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" <com> wrote in message news: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,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 Guest

  3. #3

    Default Re: String Array Manipulation Problem

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

    Garfield <com> wrote: 

    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 - <com>
    http://www.pobox.com/~skeet/
    If replying to the group, please do not mail me too
    Jon Guest

  4. #4

    Default 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 ps out everything prior to the comma, the
    second statment ps 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" <com> wrote in message
    news: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,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 Guest

  5. #5

    Default Re: String Array Manipulation Problem

    Hi Garfield,
     

    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 -
     
    an 
    the 
    two 


    Joseph Guest

  6. #6

    Default 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" <com> wrote in message
    news:exC$L$phx.gbl... 
    BAD'. 

    usefullness. 
    that 
    I'd 
    implemented 



    Michael Guest

Similar Threads

  1. Need some help with string manipulation
    By Ramesh in forum Linux / Unix Administration
    Replies: 4
    Last Post: December 5th, 07:11 PM
  2. Problem compare string with array
    By John in forum PERL Beginners
    Replies: 4
    Last Post: May 14th, 08:57 PM
  3. String manipulation
    By Jerry Preston in forum PERL Beginners
    Replies: 2
    Last Post: January 19th, 01:27 PM
  4. php array problem string to integer
    By Sébastien in forum PHP Development
    Replies: 0
    Last Post: August 3rd, 06:17 AM
  5. Replies: 2
    Last Post: July 20th, 06:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139