使用包含分隔符的元素拆分字符串

本文关键字:元素 拆分 字符串 分隔符 包含 | 更新日期: 2023-09-27 18:27:48

我有一个字符串,如下所示:

'xxx',1,'yyy',0,'zzz,www',0,'','etc'

我想用逗号分隔它,但问题是包含它的元素(字符串)。我可以逐个元素解析这个字符串,但最好使用Regex之类的东西。

若元素是字符串类型,它总是在引号中。

预期结果:

xxx
1
yyy
0
zzz,www
0
<empty>
etc

用于拆分的Oryginal字符串:DA,"t-1',3000.001500.00,1.00,13000.001500.00,1.4301-10',7.900,*'1.4301','MAX,MIN-500','C'

代码:

string SampleText = @"DA,'SHT-1',3000.00,1500.00,1.00,1,3000.00,1500.00,'1.4301-10',7.900,
*  '1.4301','MAX,MIN-500','C'";
// [1] Prepare for splitting (remove new lines, white spaces, etc);
SampleText = Regex.Replace(SampleText, @"'r'n?|'n|'*|'s", "");
// [2] replacing commas
MatchCollection mc = Regex.Matches(SampleText , "''.*?''");
foreach (Match mh in mc)
{
  if (mh.Value.Contains(','))
  {
    SampleText = SampleText.Replace(mh.Value, mh.Value.Replace(",", "_"));
  }
}
// splitting
string[] progHeader = SampleText.Split(new char[] { ',' });

我需要的是删除替换逗号的步骤。

使用包含分隔符的元素拆分字符串

您可以尝试使用类似的东西

    public static String[] SplitCsv(String value) {
      if (Object.ReferenceEquals(null, value))
        return null;
      const Char quotation = '''';
      const Char separator = ',';
      List<String> result = new List<String>();
      Boolean inQuotation = false;
      Boolean isStarted = false;
      StringBuilder Sb = new StringBuilder();
      foreach (Char Ch in value) {
        if (inQuotation) {
          Sb.Append(Ch);
          inQuotation = Ch != quotation;
          continue;
        }
        if (Ch == separator) {
          result.Add(Sb.ToString());
          Sb.Length = 0;
          isStarted = true;
        }
        else {
          Sb.Append(Ch);
          isStarted = false;
          inQuotation = Ch == quotation;
        }
      }
      if (isStarted || (Sb.Length > 0))
        result.Add(Sb.ToString());
      return result.ToArray();
    }
  ....
  var test1 = SplitCsv("1,2,3");   // <- ["1", "2", "3"]
  var test2 = SplitCsv("1,2,");    // <- ["1", "2", ""]
  var test3 = SplitCsv("1,'2',3"); // <- ["1", "'2'", "3"]
  var test4 = SplitCsv("1,'2,3'"); // <- ["1", "'2,3'"]
  var test5 = SplitCsv("1,''");    // <- ["1", "''"]

如果这只是一个练习:/('[^']*')|([^,']+)/g看起来会这样(但匹配,而不是拆分)。