String.Format对面 [英] String.Format opposite

查看:118
本文介绍了String.Format对面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

你好吗?

我正在尝试为String类构建扩展方法与String.Format相反。

I'm trying to build an extension method for the class String that does the opposite of String.Format.

String.Format("My name is {0} {1}", "John", "Doe")

这将返回"我的名字是John Doe"。

This will return "My name is John Doe".

现在让我们想象一下扩展方法:

Now let's imagine the extension method :

// Extension method
public static string[] Deformat(this string s, string input);

// Example of use
var s = "My name is John Doe";
string[] names = s.Deformat("My name is {0} {1}");

我希望名字包含一个带有"John"的字符串数组和"Doe"。

I'd like names to contain a string array with "John" and "Doe".

关于如何做到这一点的任何想法?看起来RegEx是一个很好的方法,但我一直在努力工作数小时失败。

Any idea about how to do that ? It looks like RegEx is a good approach but I has been working on that for hours unsuccessfully.

提前感谢您的帮助。

推荐答案

我很好奇如果这个字符串是"玛丽安琼斯",那该怎么办呢?和其他名称超过两个令牌的排列,你将在示例中显示你将缺少部分名称。
I'm curious how this would work say if the string was "Mary Ann Jones" and other permutations of names that are more than two tokens how you are showing in the example you would be missing part of the name.


这篇关于String.Format对面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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