By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

question about Split()

P: n/a
This is an example in the book I'm reading:

string fullName = " Edward C Koop ";
fullName = fullName.Trim();
string[] names = fullName.Split(' ');
string firstName = names[0]; // Edward

Two questions about this:

1. Why do you use single quotes with Split() instead of double? Is this
necessary?

2. Would this parse the last name, since Trim() took out the trailing
space? Isn't Split() only looking for spaces following the separate
elements (words) of the string?
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
1. Why do you use single quotes with Split() instead of double? Is this
necessary? Because ' ' says a single space char. The overload takes a char, not a
string. The new 2.0 split allows strings.
2. Would this parse the last name, since Trim() took out the trailing
space? Isn't Split() only looking for spaces following the separate
elements (words) of the string?

It splits on an space it finds. If you have two spaces in a row, you will
get empty string in the string[]. If you did not remove the last space, you
would also get and empty string as last element in the array. Using trim
avoids this. hth
--
William Stacey
Nov 17 '05 #2

P: n/a
Hi John,
Split deals with characters as a parameter not a string that is why you
use single quotes (for characters) instead of double quotes which are used
for strings.

This would parse the last name because the Split function is iterating
through the characters in the string until it finds an instance of the
character you wanted to split on, it then takes whatever is infront of that
character (which could be an empty string) as one element of the array, moves
past the split character and keeps looking for the next instance of the split
character so in your case it would be:

After the trim operation you will have "Edward C Koop"

-1st split character found
"Edward C Koop"
---------^ -> "Edward" is first element in the list

-2nd instance of split character found
"Edward C Koop"
-----------^ -> "C" is the second element in the list

-End of string reached
"Edward C Koop"
------------------^ -> "Koop" is third element in the list

Hope that helps
Mark R Dawson
http://www.markdawson.org
Edward
"John Salerno" wrote:
This is an example in the book I'm reading:

string fullName = " Edward C Koop ";
fullName = fullName.Trim();
string[] names = fullName.Split(' ');
string firstName = names[0]; // Edward

Two questions about this:

1. Why do you use single quotes with Split() instead of double? Is this
necessary?

2. Would this parse the last name, since Trim() took out the trailing
space? Isn't Split() only looking for spaces following the separate
elements (words) of the string?

Nov 17 '05 #3

P: n/a
> This would parse the last name because the Split function is iterating
through the characters in the string until it finds an instance of the
character you wanted to split on, it then takes whatever is infront of that
character (which could be an empty string) as one element of the array, moves
past the split character and keeps looking for the next instance of the split
character so in your case it would be:


But that description doesn't seem to include the situation where a word
does not have a space at the end of it, so I was wondering how it
extracts that final word. Unless it looks before *and* after a word?
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.