简体   繁体   English

如何在C#中的字符串中使用多个反向逗号按字符串拆分字符串

[英]How to split string by string with multiple inverted commas in string in c#

How to split a string by "," where " is part of string to split by. 如何用“,”分割字符串,其中“是要分割的字符串的一部分。

         string[] stringSeparator = new string[] {","};
            while (!sr.EndOfStream) {
                string strline = sr.ReadLine();
                string[] _values = strline.Split(stringSeparator, StringSplitOptions.None);

                for (int entry = 0; entry < _values.Length; entry++) {
                    MessageBox.Show(_values[entry]);
                }
            }

Tried to use "","" but it seems to return whole line instead of just part of it. 试图使用“”,“”,但是它似乎返回整行而不是一部分。

Edit: 编辑:

String to split (example): 要分割的字符串(示例):

First line: 第一行:

"24446022020000000174234443,""PLN"",""NVESTMENT SOMETHING "",""2011-03-06"",""2011-03-07"",""-25,21""" “ 24446022020000000174234443,”“ PLN”“,”“ NVESTMENT SOMETHING”“,”“ 2011-03-06”“,”“ 2011-03-07”“,”“-25,21”“”

2nd line: 第二行:

"1,""E"",""2011-03-04"",""2011-03-07"",""2011-03-07"",""1,00"",""0000000100000001"",""UZNANIE sdsd ELIXIR"",""45555550040000001244580001"",""Some Client (E)KLIENT NR:0000000100000001"",""example something"",""73116022447246000100000001""" “1”, “E”” “” 2011-03-04 “”, “” 2011-03-07 “”, “” 2011-03-07 “”, “” 1.00 “” “” 0000000100000001 “”,“” UZNANIE sdsd ELIXIR“”,“” 45555550040000001244580001“”,“”某些客户(E)KLIENT NR:0000000100000001“”,“”示例内容“”,“” 73116022447246000100000001“”“

If you want to represent literal quotation marks in a string, you need to escape it (or double it in a verbatim string literal). 如果要在字符串中表示文字引号,则需要对其进行转义(或在逐字字符串文字中加倍)。

ie,

new string[] { "\",\"" };
//or
new string[] { @""",""" };

As for why you're getting the values you were getting, consider the ways you were typing it: 至于为什么要获取值,请考虑键入方式:

string[] stringSeparator = new string[] { "," };

Is a string array containing a single string, just a comma , . 是一个字符串数组,其中包含单个字符串,只是一个逗号, It will split but you probably didn't get the values you were expecting. 它将分裂,但您可能未获得期望的值。

string[] stringSeparator = new string[] { "","" };

Is a string array containing a two strings, both empty (blank) strings. 是一个包含两个字符串(均为空(空白))的字符串数组。 Perhaps it would be clearer if it was typed as: new string[] { "", "" }; 如果将其键入为以下内容,则可能会更清楚: new string[] { "", "" }; . The Split() function ignores empty string delimiters so it doesn't split anything. Split()函数将忽略空字符串定界符,因此它不会拆分任何内容。

string[] stringSeparator = new string[] { "\",\"" };

Is a string array containing a single string, double-quote comma double-quote "," . 是包含单个字符串,双引号逗号,双引号","的字符串数组。 It should get you everything between the "," in your strings. 它应该使您在字符串中的","之间的所有内容。

Try 尝试

char[] delimiters = new char[] { ',', '"' };
string[] parts = value.Split(delimiters,
                 StringSplitOptions.RemoveEmptyEntries);

Trim first and then split to get rid of all quotes. 首先修剪,然后拆分以除去所有引号。

string[] stringSeparator = new string[] {"\",\""};
while (!sr.EndOfStream) 
{

    //trim removes first and last quote since they are not removed by the split
    string line = sr.ReadLine().Trim('"');
    string[] values = line.Split(stringSeparator, StringSplitOptions.None);

    for (int index = 0; index < values.Length; index++)
        MessageBox.Show(values[index]);
}
string strline = sr.ReadLine();              
string[] abc = strline.Split('"');
abc = Array.FindAll(abc, val => val != ",").ToArray();
string result[] = Array.FindAll(abc, val => val != "").ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM