有没有办法获取字符串的所有格式参数?
我有这个字符串:“{0} test {0} test2 {1} test3 {2:####}” 结果应该是一个列表: {0} {0} {1} {2:####}
.net 中是否有支持此功能的内置功能?
最佳答案
我没有听说过这样的内置功能,但您可以试试这个(我假设您的字符串包含以数字开头的标准格式参数):
List<string> result = new List<string>();
string input = "{0} test {0} test2 {1} test3 {2:####}";
MatchCollection matches = Regex.Matches(input, @"\{\d+[^\{\}]*\}");
foreach (Match match in matches)
{
result.Add(match.Value);
}
它返回列表中的 {0} {0} {1} {2:####}
值。对于 tehMick 的字符串,结果将是一个空集。
关于c# - 如何获取所有字符串格式参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2721443/