473,657 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2096
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
415
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
6373
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
7
1731
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 selCars.split("|") and i want to filter the dataset based on the string so i do this
5
1569
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 VB.net ain't Perl... Ken
13
1716
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 not be at the end of the line starting with ; until the end of the line including white spaces.. this is a corrected version from http://python-forum.org/py/viewtopic.php?t=1703
7
1292
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
1985
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) = split(/\|/, $listline); Here's the hard part: There are differing programs that read from the same conf file that take data stream and split it, but at some point in the future I may need to expand the variables split to. I would like to basically put...
1
2428
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 = " ", Optional ByVal Limit As Integer = -1, Optional ByVal Compare As CompareMethod = CompareMethod.Binary, Optional ByVal MaxLength As Integer = 0) As String()
2
2631
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 the information in a standard format. The main is fine and my function declarations are good also I am just having trouble writing the functions. Below are the function names and what they need to do. I have been trying some different ways to write...
0
8730
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7321
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...
0
5632
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();...
0
4151
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
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.