有效地分割格式为“ {{},{},…}”的字符串]]

问题描述 投票:2回答:5

我有以下格式的string

string instance = "{112,This is the first day 23/12/2009},{132,This is the second day 24/12/2009}"

private void parsestring(string input)
{
    string[] tokens = input.Split(','); // I thought this would split on the , seperating the {}
    foreach (string item in tokens)     // but that doesn't seem to be what it is doing
    {
       Console.WriteLine(item); 
    }
}

我想要的输出如下所示:

112,This is the first day 23/12/2009
132,This is the second day 24/12/2009

但是目前,我得到以下一个:

{112
This is the first day 23/12/2009
{132
This is the second day 24/12/2009

我是C#的新手,我们将不胜感激。

我有以下格式的字符串。字符串实例=“ {112,这是第一天23/12/2009},{132,这是第二天24/12/2009}” private void parsestring(字符串输入){string [] ...

c# split string-parsing
5个回答
4
投票

[好,如果您有一个称为ParseString的方法,则它返回一些东西是一件好事(而说它是ParseTokens可能并不坏)。因此,如果执行此操作,则可以转到以下代码


5
投票

为此使用Regex


3
投票

不要专注于Split()作为解决方案!没有它,这是一件很简单的事情。正则表达式的答案也可能还可以,但我想就原始效率而言,使“解析器”可以解决问题。


0
投票

using System.Text.RegularExpressions;添加到班级顶部


0
投票

如果您不想使用正则表达式,则以下代码将产生所需的输出。

© www.soinside.com 2019 - 2024. All rights reserved.