.NET中有'sscanf()'吗?

doe*_*man 15 .net string

.NET Framework为我们提供了Format方法:

string s = string.Format("This {0} very {1}.", "is", "funny");
// s is now: "This is very funny."
Run Code Online (Sandbox Code Playgroud)

我想要一个"Unformat"功能,例如:

object[] params = string.Unformat("This {0} very {1}.", "This is very funny.");
// params is now: ["is", "funny"]
Run Code Online (Sandbox Code Playgroud)

我知道ANSI-C库中存在类似的东西(printf vs scanf).

问题是:C#中有类似的东西吗?

更新:使用正则表达式捕获组不是我需要的解决方案.他们也是一种方式.我在寻找一个系统,该系统可以工作两种方式在一个单一的格式.放弃一些功能(如类型和格式信息)是可以的.

mqp*_*mqp 15

没有这样的方法,可能是因为解决模糊的问题:

string.Unformat("This {0} very {1}.", "This is very very funny.")
// are the parameters equal to "is" and "very funny", or "is very" and "funny"?
Run Code Online (Sandbox Code Playgroud)

针对此问题制作正则表达式捕获 ; 你可能想看看它们.


ann*_*ata 5

正则表达式与分组?

/This (.*?) very (.*?)./
Run Code Online (Sandbox Code Playgroud)


Jon*_*ood 5

如果有人有兴趣,我刚刚发布了scanf().NET 的替代品.如果正则表达式没有为你完全删除它,我的代码scanf()非常接近格式字符串.

您可以在http://www.blackbeltcoder.com/Articles/strings/a-sscanf-replacement-for-net上查看和下载我编写的代码.


end*_*ian 4

您可以执行 string[] parts = string.Split(' '),然后按示例中的索引位置 parts[1] 和 parts [3] 提取。