473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

Nov 15 '05 #1
4 4246
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*****@hotmail.com> wrote in message news:O5**************@TK2MSFTNGP12.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

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

Garfield <gb*****@hotmail.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(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 - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '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.Substring(0, Tokens.IndexOf(",")).Trim();
txtToken2.Text = Tokens.Substring(Tokens.IndexOf(",") +1).Trim();

Hope this helps,

- Joe Geretz -

"Garfield" <gb*****@hotmail.com> wrote in message
news:O5**************@TK2MSFTNGP12.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
Nov 15 '05 #4
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**************@TK2MSFTNGP12.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 15 '05 #5

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

Similar topics

16
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...
4
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...
32
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...
29
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...
5
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...
4
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...
5
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...
14
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...
22
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.