Hi
//This is normal input line which is read from csv file and i want to get value only comma separator (i,e Redhighlighted comma only)
Note:- I cant modify that line example:- remove double quote or add double quote in line
str="12,Goods,"LCD,HDD",V21,HIGH",xyz,pqr,vbc"; //Input line
//I tried this code..I have separated in string array (named as arr) using comma(,) separator.
string[] arr= str.Split(',');
//After split value:- Actual array
arr[12]
arr[Goods]
arr["LCD]
arr[HDD"]
arr[V21]
arr[HIGH"]
arr[xyz]
arr[pqr]
arr[vbc]
//After split value:- Expected array
arr[12]
arr[Goods]
arr["LCD,HDD",V21,HIGH"]
arr[xyz]
arr[pqr]
arr[vbc]
//--------
----------
//Output
// 12 Goods "LCD,HDD",V21,HIGH" xyz pqr vbc
//If it will open in Excel file then it is displaying properly as like above output, but i want that output either in string variable or string array using c# code , so give me appropriate solution for doing that output.
Thanks in advance.
//This is normal input line which is read from csv file and i want to get value only comma separator (i,e Redhighlighted comma only)
Note:- I cant modify that line example:- remove double quote or add double quote in line
str="12,Goods,"LCD,HDD",V21,HIGH",xyz,pqr,vbc"; //Input line
//I tried this code..I have separated in string array (named as arr) using comma(,) separator.
string[] arr= str.Split(',');
//After split value:- Actual array
arr[12]
arr[Goods]
arr["LCD]
arr[HDD"]
arr[V21]
arr[HIGH"]
arr[xyz]
arr[pqr]
arr[vbc]
//After split value:- Expected array
arr[12]
arr[Goods]
arr["LCD,HDD",V21,HIGH"]
arr[xyz]
arr[pqr]
arr[vbc]
//--------
----------
//Output
// 12 Goods "LCD,HDD",V21,HIGH" xyz pqr vbc
//If it will open in Excel file then it is displaying properly as like above output, but i want that output either in string variable or string array using c# code , so give me appropriate solution for doing that output.
Thanks in advance.

VulpesPosted Dec 21, 2011, 6:15 AM
Otherwise it will be difficult to do in a general way because of possible ambiguity problems as to where one field ends and the next one begins:
Datta KharadPosted Sep 29, 2012, 2:51 AM
I got alternate solution for above problem.
private ArrayList SplitOperation(ref string[] sarrSingleLine)
{
ArrayList alRes = new ArrayList();
string strTemp = string.Empty;
bool blnQuote = false;
int iQuoteCount = 0;
int iCurQuoteCount = 0;
string[] arrCurWord;
for (int iWord = 0; iWord < sarrSingleLine.Length; iWord++)
{
if (sarrSingleLine[iWord].StartsWith("\"") || blnQuote)
{
if (string.IsNullOrEmpty(strTemp))
{
strTemp = sarrSingleLine[iWord].ToString();
}
else
{
strTemp = strTemp + "," + sarrSingleLine[iWord].ToString();
}
arrCurWord = sarrSingleLine[iWord].Split('\"');
iCurQuoteCount = arrCurWord.Length - 1;
iQuoteCount = iQuoteCount + iCurQuoteCount;
if (iQuoteCount % 2 == 0)
{
alRes.Add(strTemp);
blnQuote = false;
strTemp = string.Empty;
}
else
{
blnQuote = true;
}
}
else
{
alRes.Add(sarrSingleLine[iWord]);
}
}
if (!string.IsNullOrEmpty(strTemp))
{
arrCurWord = strTemp.Split(',');
foreach (string str in arrCurWord)
{
alRes.Add("," + str);
}
MessageBox.Show("Line is not Proper Formaat Check it..." + strTemp, "SplitOperation Method", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return alRes;
}
Datta KharadPosted Dec 21, 2011, 10:56 AM
Thanku Sir for giving desired solution.
I have all rows have same number of fields and that only one field but that field may be present some rows and may not be present some rows.....doesn't matter in that cases i will get as it is line and lastly it is working properly.
Again Thanks Sir.
Datta KharadPosted Dec 21, 2011, 12:41 AM
format can be change...suppose csv file has 7 rows and each row has different format.
Example:-
USD,5,Goods,CP188895-XXR,"LCD, 14.1"",SXGA+ - REPAIRED",Each,15.00
USD,6,Goods,CP191238-XXR,"LCD,13.3,XGA""(Glare)-REPAIRED",Each,26.00
USD,7,Goods,CP235669-XXR,"LCD, 17"""", SAMSUNG - REPAIRED",Each,20.00
USD,8,Goods,CP238801-XXR,"LCD, 12IN, XGA, H - REPAIRED",Each,60.00
USD,1,Goods,CP051250-XXR,"PCB,MOTHERBOARD PC-200 - REPAIRED",Each,1.00
USD,3,Goods,CP112545-XXR,"LCD,W/TOUCHPANEL,10.4"",TFT/XGA - REPAIRED",Each,4.00
USD,4,Goods,CP115323-XXR,"LCD,13.3"",SANYO XGA - REPAIRED",Each,1.00
//Output
USD 5 Goods CP188895-XXR "LCD, 14.1"",SXGA+ - REPAIRED" Each 15.00
USD 6 Goods CP191238-XXR "LCD,13.3,XGA""(Glare)-REPAIRED" Each 26.00
USD 7 Goods CP235669-XXR "LCD, 17"""", SAMSUNG - REPAIRED" Each 20.00
USD 8 Goods CP238801-XXR "LCD, 12IN, XGA, H - REPAIRED" Each 60.00
USD 1 Goods CP051250-XXR "PCB,MOTHERBOARD PC-200 - REPAIRED" Each 1.00
USD 3 Goods CP112545-XXR "LCD,W/TOUCHPANEL,10.4"",TFT/XGA - REPAIRED" Each 4.00
USD 4 Goods CP115323-XXR "LCD,13.3"",SANYO XGA - REPAIRED" Each 1.00
After separated and collect it string or stringbuilder these above output i want to save into database. So how can separate these data properly b'coz Open Office software (Excel) is displaying properly..... Please give me solution, its urgent.
VulpesPosted Dec 20, 2011, 11:20 AM
using System;
class Test
{
static void Main()
{
string str="12,Goods,\"LCD,HDD\",V21,HIGH\",xyz,pqr,vbc";
// assume embedded commas will always be in the third of six fields
string[] arr = new string[6];
string[] temp = str.Split(',');
int count = temp.Length;
arr[0] = temp[0];
arr[1] = temp[1];
arr[2] = String.Join(",", temp, 2, count - 5);
arr[3] = temp[count - 3];
arr[4] = temp[count - 2];
arr[5] = temp[count - 1];
// check it worked
foreach(string s in arr) Console.Write("{0}\t", s);
Console.ReadKey();
}
VulpesPosted Dec 20, 2011, 10:56 AM
Pravin GhadgePosted Dec 20, 2011, 10:34 AM
May i know, is this same format will remain same for all data.