我有以下格式的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 [] ...
[好,如果您有一个称为ParseString
的方法,则它返回一些东西是一件好事(而说它是ParseTokens
可能并不坏)。因此,如果执行此操作,则可以转到以下代码
为此使用Regex
:
不要专注于Split()作为解决方案!没有它,这是一件很简单的事情。正则表达式的答案也可能还可以,但我想就原始效率而言,使“解析器”可以解决问题。
将using System.Text.RegularExpressions;
添加到班级顶部
如果您不想使用正则表达式,则以下代码将产生所需的输出。