C # - Separate / cut string with commas (but with monetary value included in the string)

1

I have a problem reading a txt file that has its columns separated by a comma but also has monetary values that are being cut and should not, because they are in quotation marks. Here is an example line:

Nome, Idade, Valor a receber, Valor pendente, descricao
Teste, 25, "1.234,30", "987,90", teste

After you have read the file

string[] todasLinhas = File.ReadAllLines(arquivo, Encoding.GetEncoding("iso-8859-1"));

I try to cut by comma

foreach (string linha in todasLinhas)
{
   string[] colunas = linha.Split(',');
}

The result looks like this:

Teste
25
"1234
30"
"987
90"
teste

Is there any way to make Split not cut when the character is enclosed in quotation marks? or is there any other solution you can share?

    
asked by anonymous 19.05.2016 / 22:04

1 answer

3

For your case of your exeplo I recommend doing split comma with space:

string[] colunas = linha.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

If you prefer to use Regex , you can split by commas not followed by number or letter (if words also have a comma), but it will be less efficient and for your example the result is the same:

string[] colunas = new System.Text.RegularExpressions.Regex(@",[^\d\w]").Split(linha);

If the columns are separated only by a comma, you can give split by comma and then concatenate the positions that begin with double quotation marks:

var colunas = new List<string>();
var linhasSplit = linha.Split(',');
for (int i = 0; i < linhasSplit.Length; i++)
{
    if(linhasSplit[i][0] == '"')
        colunas.Add(linhasSplit[i] + linhasSplit[++i]);
    else
        colunas.Add(linhasSplit[i]);
}
    
19.05.2016 / 22:07