473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 17 '05 #1
5 1501
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" <gb*****@hotmai l.com> wrote in message news:O5******** ******@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

Nov 17 '05 #2
[If you could wrap your posts at about 70 characters, it would make
them easier to reply to.]

Garfield <gb*****@hotmai l.com> 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(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.


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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 17 '05 #3
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" <gb*****@hotmai l.com> wrote in message
news:O5******** ******@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
Nov 17 '05 #4
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,
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" <gb*****@hotmai l.com> wrote in message
news:O5******** ******@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

Nov 17 '05 #5
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" <jg*****@nospam .com> wrote in message
news:ex******** ******@TK2MSFTN GP12.phx.gbl...
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.)

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1558
by: Sean Williams | last post by:
I have been working in this problem for weeks and I was determined to sort it myself however I feel I need guidance to proceed forward. What I am trying to produce is some asp code that can split categories and group from a text string that is delimited by a "| ". (see string examples below). I have managed to find some javascript that will handle the different levels of categories without having to keep refreshing the page. So I want...
16
382
by: zohaibbaloch | last post by:
how to manipulate string without using arrays.manipulation example i have to print dog or any string i want to cut d from dog and paste it a end and add a at the last dog becoms ogda. please help
4
2268
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some values and separators class { private string globalvar = ""; private void manipulate (whattodo ) //substring, join, etc....
4
4260
by: Garfield | last post by:
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
32
14852
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
29
4309
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
4
3482
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if substrings need be replaced, or one character needs be changed, what shall I do? Is it better to convert strings to UCS-32 before manipulation? But on Windows, wchar_t is 16 bits which isn't enough for characters which can't be simply encoded...
5
1723
by: Cleverbum | last post by:
I'm not really accustomed to string manipulation and so I was wondering if any of you could be any help i speeding up this script intended to change the format of some saved log information into a CSV file while removing duplicate records. The main problem is that the script currently takes about 20 seconds to execute, and were it to take much longer it would time out. Below is the script itself, and then some example lines from the log...
14
1759
by: aqazi | last post by:
Hi folks I am trying to write a program which acts like a p2p server. When the program starts it reads a file from whereit will read broadcast IP address, port and another port number. Now I am trying skip the comments and empty lines by saying if there is newline or # sign at the first charecter of a line then to skip the line.But the problem is the program is not working. I am using the following code:
22
2602
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.