I'd like some help splitting a string please. My data is CSV, has quotes around each field, and there are actual commas in the data. So a plain split on a commas will not work.
Here is an example line of data
"SKU","Description","Price"
"3030495","This is a great product, at a great price","100.00"
Thanks for any help. I've been wrestling with this for awhile.
Patrick
asp.netC#
PatrickAllmond.com. Information Technology Solutions Helping You Focus. Helping You Succeed.
Sorry about that. I was thinking of the String.Replace method. It looks like your best bet is to use a regular expression. See
http://www.regexlib.com for more help with regular expressions.
string s = @"""3030495"",""This is a great product, at a great price"",""100.00""";
Regex reg = new Regex(@""",""", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string[] arr = reg.Split(s.Trim('"'));
foreach(string current in arr){
Console.WriteLine(">>" + current);
}
Would it handle the delimiter being in the data as well as delimiting it? In the examples we've given above there are commas in my data, and the comma is the delimiter also. Quotes surround the data that has the commas in it.
Example: "Product Name", "Product Descriptions, sometimes with commas", 8.99, 23.44, "And possibly even some more text there"
PatrickAllmond.com. Information Technology Solutions Helping You Focus. Helping You Succeed.
Member
83 Points
244 Posts
How do I split this?
Oct 07, 2006 10:23 PM|patrick24601|LINK
I'd like some help splitting a string please. My data is CSV, has quotes around each field, and there are actual commas in the data. So a plain split on a commas will not work.
Here is an example line of data
"SKU","Description","Price"
"3030495","This is a great product, at a great price","100.00"
Thanks for any help. I've been wrestling with this for awhile.
Patrick
asp.net C#
Information Technology Solutions
Helping You Focus. Helping You Succeed.
Contributor
6294 Points
5754 Posts
ASPInsiders
Re: How do I split this?
Oct 07, 2006 10:58 PM|StrongTypes|LINK
Member
83 Points
244 Posts
Re: How do I split this?
Oct 07, 2006 11:06 PM|patrick24601|LINK
Information Technology Solutions
Helping You Focus. Helping You Succeed.
Contributor
6294 Points
5754 Posts
ASPInsiders
Re: How do I split this?
Oct 07, 2006 11:12 PM|StrongTypes|LINK
Sorry about that. I was thinking of the String.Replace method. It looks like your best bet is to use a regular expression. See http://www.regexlib.com for more help with regular expressions.
Contributor
6294 Points
5754 Posts
ASPInsiders
Re: How do I split this?
Oct 07, 2006 11:20 PM|StrongTypes|LINK
Actually, now that I think about it, there is an overload of the String.Split method you could use.
string csv = "\"1\",\"2\",\"3\"";
string[] matches = new string[] {"\",\""};
string[] csvArray = csv.Split(matches, StringSplitOptions.None);
csvArray[0] = csvArray[0].Replace("\"", string.Empty);
csvArray[2] = csvArray[2].Replace("\"", string.Empty);
string string1 = csvArray[0];
string string2 = csvArray[1];
string string3 = csvArray[2];
Member
83 Points
244 Posts
Re: How do I split this?
Oct 07, 2006 11:39 PM|patrick24601|LINK
Thanks. I was also thinking I could just do a .Replace on "," with | or ~ and split on that single character.
Information Technology Solutions
Helping You Focus. Helping You Succeed.
Member
83 Points
244 Posts
Re: How do I split this?
Oct 17, 2006 12:48 AM|patrick24601|LINK
Now I need some additional help if anybody could spare it :)
I found another datafeed I have to import where all of textual/string items have quotes, but the numbers do not:
"Product Name","Product HTML Description",8.99,4.99
How would I handle that one?
Thanks,
Patrick
Information Technology Solutions
Helping You Focus. Helping You Succeed.
All-Star
24872 Points
4336 Posts
ASPInsiders
Moderator
MVP
Re: How do I split this?
Oct 17, 2006 01:07 AM|vcsjones|LINK
Member
83 Points
244 Posts
Re: How do I split this?
Oct 17, 2006 08:34 AM|patrick24601|LINK
I am not sure if you saw some of my earlier posts, but my textual data can have commas in it also. So a better example of my data is:
"Product Name","Product Description, which can have commas",8.99,34.33,"Excellent Rating"
Information Technology Solutions
Helping You Focus. Helping You Succeed.
All-Star
24872 Points
4336 Posts
ASPInsiders
Moderator
MVP
Re: How do I split this?
Oct 17, 2006 12:17 PM|vcsjones|LINK
Member
83 Points
244 Posts
Re: How do I split this?
Oct 17, 2006 12:45 PM|patrick24601|LINK
Information Technology Solutions
Helping You Focus. Helping You Succeed.
Member
50 Points
51 Posts
Re: How do I split this?
Oct 17, 2006 04:19 PM|vladi|LINK
Member
83 Points
244 Posts
Re: How do I split this?
Oct 17, 2006 05:20 PM|patrick24601|LINK
Kevin,
It did not work. WHen it encountered a comma in the middle of my data it split it there.
"text field 1","Text field 2, plus some more" <--- it splits right after the 2.
Information Technology Solutions
Helping You Focus. Helping You Succeed.
All-Star
24872 Points
4336 Posts
ASPInsiders
Moderator
MVP
Re: How do I split this?
Oct 17, 2006 05:24 PM|vcsjones|LINK
In that case you would have to use a regular expression to split it.
Member
83 Points
244 Posts
Re: How do I split this?
Oct 17, 2006 06:41 PM|patrick24601|LINK
Information Technology Solutions
Helping You Focus. Helping You Succeed.
Member
83 Points
244 Posts
Re: How do I split this?
Oct 17, 2006 06:53 PM|patrick24601|LINK
I am going to look into using this:
http://www.codeproject.com/cs/database/CsvReader.asp
Information Technology Solutions
Helping You Focus. Helping You Succeed.
Member
83 Points
244 Posts
Re: How do I split this?
Oct 17, 2006 07:14 PM|patrick24601|LINK
Information Technology Solutions
Helping You Focus. Helping You Succeed.
Contributor
3938 Points
3276 Posts
Re: How do I split this?
Oct 18, 2006 12:56 PM|JeffreyABecker|LINK
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
Member
83 Points
244 Posts
Re: How do I split this?
Oct 18, 2006 01:41 PM|patrick24601|LINK
Would it handle the delimiter being in the data as well as delimiting it? In the examples we've given above there are commas in my data, and the comma is the delimiter also. Quotes surround the data that has the commas in it.
Example: "Product Name", "Product Descriptions, sometimes with commas", 8.99, 23.44, "And possibly even some more text there"
Information Technology Solutions
Helping You Focus. Helping You Succeed.