<分区>
Possible Duplicate:
Is there an equivalent to 'sscanf()' in .NET?
C 中的 sscanf 是从字符串中读取格式良好的输入的好方法。
如何实现这个C#。
例如,
int a,b;
char *str= "10 12";
sscanf(str,"%d %d",&a,&b);
上面的代码会将 10 分配给 a,将 12 分配给 b。
如何使用 C# 实现同样的效果?
标签 c# c string formatted-input
<分区>
Possible Duplicate:
Is there an equivalent to 'sscanf()' in .NET?
C 中的 sscanf 是从字符串中读取格式良好的输入的好方法。
如何实现这个C#。
例如,
int a,b;
char *str= "10 12";
sscanf(str,"%d %d",&a,&b);
上面的代码会将 10 分配给 a,将 12 分配给 b。
如何使用 C# 实现同样的效果?
最佳答案
在 C# 中没有直接的等价物。在 C# 中给定相同的任务,您可以这样做:
string str = "10 12";
var parts = str.Split(' ');
int a = Convert.ToInt32(parts[0]);
int b = Convert.ToInt32(parts[1]);
根据您可以假设输入的格式如何,您可能需要添加一些错误检查。
关于C# 等效于 C sscanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4223917/