![](/img/trans.png)
[英]What is the difference between a lambda expression and a predicate in .NET?
[英]Regex substitution: what is the difference between $& and Value (in lambda expression)?
假设我想使用正则表达式在字符串中的每个单词之前添加“test”。
string MyText="hello world"
string Pattern = "\w+";
我能做到这一点:
Regex.Replace(MyText, Pattern, "test$&")
或这个:
Regex.Replace(MyText, Pattern, m=>"test"+m.Value)
我会得到相同的结果,那么lambda表达式中$&和Value之间的区别是什么? 如果结果没有差异,是否存在性能问题?
Lambdas是匿名方法,与常规方法相同。 在您的示例中声明的那个等效于: string Convert(RegexMatch match) { return "test" + match.Value; }
string Convert(RegexMatch match) { return "test" + match.Value; }
。 使用此语法可以让您访问更丰富的可能性,然后使用第一个示例中使用的Regex表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.