我该怎么办的sscanf在c# [英] how do I do sscanf in c#

查看:358
本文介绍了我该怎么办的sscanf在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在C#中做到这一点。

how do I do this in 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?

我使用C#4.0

推荐答案

如果像scannf你愿意假设用户。将给出完全正确的数据,那么你可以做以下

If like scannf you are willing to assume that users will give completely correct data then you can do the following.

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.

这篇关于我该怎么办的sscanf在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆