基于模式的字符串解析 [英] Pattern based string parse
本文介绍了基于模式的字符串解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,当我需要通过用逗号将它们连起来来对某些值进行分类时,我会这样做:
When I need to stringify some values by joining them with commas, I do, for example:
string.Format("{0},{1},{3}", item.Id, item.Name, item.Count);
并且具有例如"12,Apple,20"
.
然后我想做相反的操作,从给定的字符串中获取值.像这样:
And have, for example, "12,Apple,20"
.
Then I want to do opposite operation, get values from given string. Something like:
parseFromString(str, out item.Id, out item.Name, out item.Count);
我知道,这在C语言中是可能的.但是我不知道C#中的这种功能.
I know, it is possible in C. But I don't know such function in C#.
推荐答案
是的,这很容易.您只需使用 String.Split
方法在每个逗号上拆分字符串
Yes, this is easy enough. You just use the String.Split
method to split the string on every comma.
例如:
string myString = "12,Apple,20";
string[] subStrings = myString.Split(',');
foreach (string str in subStrings)
{
Console.WriteLine(str);
}
这篇关于基于模式的字符串解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文