String Array Manipulation Problem

G

Garfield

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
 
O

ozgur develioglu

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.

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
 
J

Jon Skeet

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

Garfield said:
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);
 
J

Joseph Geretz

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 -

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
 
J

Joseph Geretz

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

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
 
M

Michael Lang

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top