【发布时间】:2020-01-27 02:55:53
【问题描述】:
我有一个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# 的新手,如有任何帮助,我们将不胜感激。
【问题讨论】:
-
如果格式真的那么简单,分割
"},{"作为分隔符,然后从结果数组的第一项中删除孤儿{,并从结果数组中的最后一项。 -
@SaniSinghHuttunen 你能帮帮我吗?
-
您可以使用 TextFieldParser 类,即使它是在 VisualBasic 空间中定义的。 docs.microsoft.com/en-us/dotnet/api/… 另外,如果你的内部文本不包含双引号,那么你可以用双引号替换大括号,然后使用文本字段解析器忽略引号
-
@EdPlunkett 当我在拆分方法中使用
"},{"时,出现错误无法从字符串转换为字符。我不确定其中有什么问题? -
@Analia
string[] tokens = input.Split("},{");为我工作。
标签: c# split string-parsing