Spliting a column in a dataset row

How do I split a string in column in a dataset row

Page 1 of 1

13 Replies - 2849 Views - Last Post: 20 November 2008 - 12:26 PM Rate Topic: -----

#1 tavo2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 08

Spliting a column in a dataset row

Posted 19 November 2008 - 09:01 PM

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:

string clientName = dataRow["Client Location"].ToString();


Is This A Good Question/Topic? 0
  • +

Replies To: Spliting a column in a dataset row

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 09:10 PM

Use the Split method of the String class to return an array. The following will split on the hyphen symbol.

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?
Was This Post Helpful? 0
  • +
  • -

#3 tavo2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 08

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 09:17 PM

View PostJayman, on 19 Nov, 2008 - 08:10 PM, said:

Use the Split method of the String class to return an array. The following will split on the hyphen symbol.

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.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 09:19 PM

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];


Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 10:08 PM

@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.
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];




View Posttavo2k4, on 19 Nov, 2008 - 07:17 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#6 tavo2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 08

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 10:36 PM

Thank you Jayman, this will work
Was This Post Helpful? 0
  • +
  • -

#7 tavo2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 08

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 11:07 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 tavo2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 08

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 11:25 PM

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.
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 11:30 PM

I would use the Substring Method passing in where to start and how many characters you want, like this

string zipInfo = "76065 warranty";
string zip = zipInfo.Substring(0, 5)



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Spliting a column in a dataset row

Posted 19 November 2008 - 11:32 PM

see http://msdn.microsof...y/aka44szs.aspx
Was This Post Helpful? 0
  • +
  • -

#11 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Spliting a column in a dataset row

Posted 20 November 2008 - 06:06 AM

if array[3] is your zip code you can do this

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


Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Spliting a column in a dataset row

Posted 20 November 2008 - 09:53 AM

Topics merged.
Was This Post Helpful? 0
  • +
  • -

#13 tavo2k4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 08

Re: Spliting a column in a dataset row

Posted 20 November 2008 - 11:45 AM

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.
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Spliting a column in a dataset row

Posted 20 November 2008 - 12:26 PM

View Posttavo2k4, on 20 Nov, 2008 - 01:45 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1