473,566 Members | 2,770 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 15 '05 #1
4 4257
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 15 '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 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.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 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******** ******@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 15 '05 #5

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

Similar topics

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
2267
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....
32
14783
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
4293
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...
5
1496
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
4
3476
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...
5
1717
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...
14
1752
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...
22
2579
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
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
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...
0
5213
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...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
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
0
925
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...

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.