What's Here?
Members: 244,308
Replies: 693,190
Topics: 113,170
Snippets: 3,863
Tutorials: 935
Total Online: 805
Members: 55
Guests: 750
Loading. Please Wait...
Spliting a column in a dataset row
Spliting a column in a dataset row, How do I split a string in column in a dataset row
tavo2k4
19 Nov, 2008 - 08:01 PM
New D.I.C Head
Joined: 19 Nov, 2008
Posts: 6
Hello,
I have a DataSet of rows, I have a field or a column in each row that I wish to split. The string in this column is separated by special symbols, for example "Name - Tampa/FL - 43188". I want to split this string extracting "Name", "City", "Sate" and "Zip" into different strings.
Here is an example of the row and field:
CODE
string clientName = dataRow["Client Location"].ToString();
tavo2k4
RE: Spliting A Column In A Dataset Row 19 Nov, 2008 - 08:17 PM
New D.I.C Head
Joined: 19 Nov, 2008
Posts: 6
QUOTE(Jayman @ 19 Nov, 2008 - 08:10 PM)
Use the Split method of the String class to return an array. The following will split on the hyphen symbol.
CODE
string[] clientName = dataRow["Client Location"].ToString().Split('-');
Any particular reason the data is stored in a single column in the dataset, instead of each piece of data being stored in its own column?
Well, I applied to a company that gave me a project to read a CSV file and save it into a SQL table. One of the requirements is to split this column.
Jayman
RE: Spliting A Column In A Dataset Row 19 Nov, 2008 - 09:08 PM
Student of Life
Joined: 26 Dec, 2005
Posts: 8,063
Thanked: 159 times
Dream Kudos: 500
Expert In: Everything
My Contributions
@eclipsed4utoo: If you need to split on several different characters. Then this will work equally well and is easier to maintain and more readable. This way you only need one array.
CODE
string[] array = dataRow["Client Location"].ToString().Split(new char[] { '-', '/' }); string name = array[0]; string city = array[1]; string state = array[2]; string zip = array[3];
QUOTE(tavo2k4 @ 19 Nov, 2008 - 07:17 PM)
Well, I applied to a company that gave me a project to read a CSV file and save it into a SQL table. One of the requirements is to split this column.
I see.
tavo2k4
RE: Spliting A Column In A Dataset Row 19 Nov, 2008 - 09:36 PM
New D.I.C Head
Joined: 19 Nov, 2008
Posts: 6
Thank you Jayman, this will work
tavo2k4
RE: Spliting A Column In A Dataset Row 19 Nov, 2008 - 10:07 PM
New D.I.C Head
Joined: 19 Nov, 2008
Posts: 6
Ok, now I have a string that has the zip code and some text at the end, for example "76065 warranty". I need to clean this string, do I put it in array and do a "for each" or trim it. I just want the first five numbers.
tavo2k4
RE: Spliting A Column In A Dataset Row 19 Nov, 2008 - 10:25 PM
New D.I.C Head
Joined: 19 Nov, 2008
Posts: 6
Ok, now I have a string that has the zip code and some text at the end, for example "76065 warranty". I need to clean this string, do I put it in array and do a "for each" or trim it. I just want the first five numbers.
PsychoCoder
RE: Spliting A Column In A Dataset Row 19 Nov, 2008 - 10:30 PM
loves.Coding(this);
Joined: 26 Jul, 2007
Posts: 12,288
Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery
My Contributions
I would use the
Substring Method passing in where to start and how many characters you want, like this
csharp
string zipInfo = "76065 warranty"; string zip = zipInfo.Substring(0, 5)Hope that helps
tavo2k4
RE: Spliting A Column In A Dataset Row 20 Nov, 2008 - 10:45 AM
New D.I.C Head
Joined: 19 Nov, 2008
Posts: 6
I am new to this Forum, but I can tell you that so far I got great answers for all my questions.
You guys rule, you are better than
http://forums.asp.net/ . I feel like no matter how dumb my question is I get quick and great response.
Keep up the good job, and many thanks to every body who answered me.
eclipsed4utoo
RE: Spliting A Column In A Dataset Row 20 Nov, 2008 - 11:26 AM
D.I.C Lover
Joined: 21 Mar, 2008
Posts: 1,170
Thanked: 117 times
Dream Kudos: 125
My Contributions
QUOTE(tavo2k4 @ 20 Nov, 2008 - 01:45 PM)
I am new to this Forum, but I can tell you that so far I got great answers for all my questions.
You guys rule, you are better than
http://forums.asp.net/ . I feel like no matter how dumb my question is I get quick and great response.
Keep up the good job, and many thanks to every body who answered me.
We were all noobs at one time or another.
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month