C# 等效于 C sscanf

标签 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/

相关文章:

c# - 使用一次性对象的类的层次结构。在所有这些上实现 IDisposable?

c - 避免在 C 程序中使用 main(入口点)

php - 使用 sscanf 剥离 PHP 中的封装字符

linux - 比较字符串时出现问题 (BASH)

c# - 使用 dispose/finalize 模式释放 socket/event/ummaged 代码的正确技术

c# - 当作为 Windows 服务运行时,如何让程序自己截屏?

C# - x.ToString ("D") 和 x.ToString() 之间有什么区别吗,其中 x 是 int 类型?

c - SDL_Thread 的存储大小未知?

c - DPDK发送自定义pkt但收不到

python - 查找包含子字符串的列并替换它 - Pandas