473,386 Members | 1,791 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,386 software developers and data experts.

question about Split()

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
3 2076
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: alexk | last post by:
I've a simple question. Why the following: words = "123#@$#$@^% wordB#@$".split('~`!@#$%^&*()_+-={},./') doesn't work? The length of the result vector is 1. I'm using ActivePython 2.4 Alex
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
7
by: Mike | last post by:
I'm trying to filter a dataset on items that are being passed in via a querystring. the string looks like this. chevy|ford|BMW| on the information page i split the string such as...
5
by: KC | last post by:
Can I do a split() on a string where the delimiter is one or more space characters? I have a string with spaces between the numbers but I don't know how many. You can do this easy in Perl, but...
13
by: Eric_Dexter | last post by:
All I am after realy is to change this reline = re.line.split('instr', '/d$') into something that grabs any line with instr in it take all the numbers and then grab any comment that may or may...
7
by: Matthias Winterland | last post by:
Hi, I have a simple question. When I read in a string like: a='1,2,3,4,5 6 7,3,4', can I get the list l= with a single split-call? Thx, Matthias
9
by: dtex23 | last post by:
Good afternoon all, Question about list assignment in perl... Given a piece of code that looks kinda like this for parsing some '|' delimited input text lines : ($var1, $var2, $var3) =...
1
by: John | last post by:
Hi I have written a Split function which in turn calls the standard string split function. Code is below; Function Split1(ByVal Expression As String, Optional ByVal Delimiter As String = " ",...
2
by: gdarian216 | last post by:
I am writting a program that has three different files and is compiled by a Makefile. The goal is to take a file of text and split it up in different sections and stored in vectors. Then it outputs...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.