[英]how do I do sscanf in c#
how do I do this in C#? 我怎么在C#中做到这一点?
int a,b,c;
sscanf(astring,"%d %d %d",&a,&b,&c);
minimal code and dependencies is preferable, is there some built in regex stuff? 最小的代码和依赖是更好的,有一些内置的正则表达式的东西?
I'm using c# 4.0 我正在使用c#4.0
If like scannf you are willing to assume that users will give completely correct data then you can do the following. 如果像scannf一样,您愿意假设用户将提供完全正确的数据,那么您可以执行以下操作。
string astring = ...;
string[] values = astring.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
int a = Int32.Parse(values[0]);
int b = Int32.Parse(values[1]);
int c = Int32.Parse(values[2]);
Regular expressions would work for this scenario but they are a bit overkill. 正则表达式适用于这种情况,但它们有点矫枉过正。 The string can easily be tokenized with the aforementioned Split method. 可以使用上述Split方法轻松地对字符串进行标记。
The simplest thing I can come up with is this: 我能想到的最简单的事情就是:
var fields = astring.Split(null, StringSplitOptions.RemoveEmptyEntries);
a = int.Parse(fields[0]);
b = int.Parse(fields[1]);
c = int.Parse(fields[2]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.