Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C# Expert!

Join 244,308 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 805 people online right now. Registration is fast and FREE... Join Now!




Spliting a column in a dataset row

 
Reply to this topicStart new topic

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
Post #1

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();


User is offlineProfile CardPM
+Quote Post


Jayman
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 08:10 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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?
User is offlineProfile CardPM
+Quote Post

tavo2k4
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 08:17 PM
Post #3

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.
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 08:19 PM
Post #4

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,170



Thanked: 117 times
Dream Kudos: 125
My Contributions
csharp

string[] array = dataRow["Client Location"].ToString().Split(new char[]{'-'});
string name = array[0];
string zip = array[2];

string[] secondArray = array[1].Split(new char[]{'/'});

string city = secondArray[0];
string state = secondArray[1];

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 09:08 PM
Post #5

Student of Life
Group Icon

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.

User is offlineProfile CardPM
+Quote Post

tavo2k4
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 09:36 PM
Post #6

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 6

Thank you Jayman, this will work
User is offlineProfile CardPM
+Quote Post

tavo2k4
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 10:07 PM
Post #7

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.
User is offlineProfile CardPM
+Quote Post

tavo2k4
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 10:25 PM
Post #8

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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 10:30 PM
Post #9

loves.Coding(this);
Group Icon

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 smile.gif
User is offlineProfile CardPM
+Quote Post

n8wxs
RE: Spliting A Column In A Dataset Row
19 Nov, 2008 - 10:32 PM
Post #10

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 223 times
My Contributions
see http://msdn.microsoft.com/en-us/library/aka44szs.aspx
User is offlineProfile CardPM
+Quote Post

zakary
RE: Spliting A Column In A Dataset Row
20 Nov, 2008 - 05:06 AM
Post #11

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 423



Thanked: 12 times
Dream Kudos: 175
My Contributions
if array[3] is your zip code you can do this

csharp

// this will remove all characters start at position 5
// so 54321 test will be 54321
string zip = array[3].Remove(5);


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Spliting A Column In A Dataset Row
20 Nov, 2008 - 08:53 AM
Post #12

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Topics merged.
User is offlineProfile CardPM
+Quote Post

tavo2k4
RE: Spliting A Column In A Dataset Row
20 Nov, 2008 - 10:45 AM
Post #13

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.
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Spliting A Column In A Dataset Row
20 Nov, 2008 - 11:26 AM
Post #14

D.I.C Lover
Group Icon

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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:57PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month