Answered by:
How to split string with ","

Question
-
I have string like this: "sometext1,text2 ","sometext, with commas","somenumbers","somenumbers"
How can I split them? I can not do split(','); bevause sometext1,text2 contains a comma. I Need to do something like Split('","'); but I dont know how to use double qoutation...Wednesday, March 10, 2010 9:40 AM
Answers
-
Salve Gaius,
I suppose you're really looking for what is usually being refered to as the text-qualifier and the field-delimiter. That is, your field-delimiter is a comma, but that character can be contained within the fields themselves, hence the need for a text-qualifier (a double quote in this case).
So, if I got you right, the string you posted is actually a single string and you need to retrieve the stuff between double-quotes. If that's right, here's a very simplistic way of splitting this up:
List<string> SplitUpString(string str) { string[] arrstrSeparators = new string[] { "\",\"" }; string[] arrstrItems = str.Substring(1,str.Length-2).Split(arrstrSeparators, StringSplitOptions.RemoveEmptyEntries); return arrstrItems.ToList<string>(); }
Taking the above, you could split up your string like this:
string strTest = @"""sometext1,text2 "",""sometext, with commas"",""somenumbers"",""somenumbers"""; List<string> lststrItems = SplitUpString(strTest); System.Text.StringBuilder sbMsg = new System.Text.StringBuilder(); foreach (string str in lststrItems) sbMsg.Append(str + Environment.NewLine); MessageBox.Show("Items found:" + Environment.NewLine + sbMsg.ToString());
Note that this is a very quick & dirty approach - it will only work if a) all fields are qualified by a double quote and b) the first field starts resp. the last field ends with a double quote. If you need more flexibility (i.e. in the case some fields come without text-qualifiers), you'll find various helpers on the web if you google for something like "split string text-qualifier field-delimiter", i.e. this CodeProject article .
Cheers,
Olaf
http://blogs.intuidev.com- Proposed as answer by Kenneth Haugland Wednesday, March 10, 2010 10:17 PM
- Marked as answer by Linda Liu Tuesday, March 16, 2010 9:02 AM
Wednesday, March 10, 2010 12:04 PM
All replies
-
dim f as List(of string)
dim MyString as String() = "sometext1,text2 ","sometext, with commas","somenumbers","somenumbers"
for each str as string in MyString
f.Add(str)
next
KennethWednesday, March 10, 2010 10:08 AM -
hi,
string[] strArray = new string[] { "sometext1,text2 ", "sometext, with commas", "somenumbers", "somenumbers" }; foreach(string str in strArray ) { //if u want string in a array then here is ur output //output here is - sometext1,text2 MessageBox.Show(str); //if u want to split the string in a string then string[] strSplitString = str.Split(','); for (int i = 0; i < strSplitString.Length; i++) { MessageBox.Show(strSplitString[i]); } }
Nagarjuna DilipWednesday, March 10, 2010 11:13 AM -
Salve Gaius,
I suppose you're really looking for what is usually being refered to as the text-qualifier and the field-delimiter. That is, your field-delimiter is a comma, but that character can be contained within the fields themselves, hence the need for a text-qualifier (a double quote in this case).
So, if I got you right, the string you posted is actually a single string and you need to retrieve the stuff between double-quotes. If that's right, here's a very simplistic way of splitting this up:
List<string> SplitUpString(string str) { string[] arrstrSeparators = new string[] { "\",\"" }; string[] arrstrItems = str.Substring(1,str.Length-2).Split(arrstrSeparators, StringSplitOptions.RemoveEmptyEntries); return arrstrItems.ToList<string>(); }
Taking the above, you could split up your string like this:
string strTest = @"""sometext1,text2 "",""sometext, with commas"",""somenumbers"",""somenumbers"""; List<string> lststrItems = SplitUpString(strTest); System.Text.StringBuilder sbMsg = new System.Text.StringBuilder(); foreach (string str in lststrItems) sbMsg.Append(str + Environment.NewLine); MessageBox.Show("Items found:" + Environment.NewLine + sbMsg.ToString());
Note that this is a very quick & dirty approach - it will only work if a) all fields are qualified by a double quote and b) the first field starts resp. the last field ends with a double quote. If you need more flexibility (i.e. in the case some fields come without text-qualifiers), you'll find various helpers on the web if you google for something like "split string text-qualifier field-delimiter", i.e. this CodeProject article .
Cheers,
Olaf
http://blogs.intuidev.com- Proposed as answer by Kenneth Haugland Wednesday, March 10, 2010 10:17 PM
- Marked as answer by Linda Liu Tuesday, March 16, 2010 9:02 AM
Wednesday, March 10, 2010 12:04 PM