C#反转字符串中的所有号码? [英] C# Reverse all numbers in string?
本文介绍了C#反转字符串中的所有号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串:
您好 7866592 这是我的 12432 字符串和 823 我需要翻转所有的 123 的
我要为
您好 2956687 这是我的 23421 字符串和 328 我需要翻转所有的 321 的
我用这个普通的前pression让所有的数字:
正则表达式NUMS =新的正则表达式(\ D +);
解决方案
VAR replacedString =
Regex.Replace(//查找所有的匹配,并将其替换
我们正在与MyString的,//字符串
@\ D +,//正规EX pression匹配做替代
M =>新的字符串(m.Value.Reverse()的ToArray())); //一个lambda EX pression这
//被转换为MatchEvaluator委托,所以一旦找到匹配,它
//替换为该方法的输出。
I have a string:
"Hello 7866592 this is my 12432 string and 823 i need to flip all 123"
And i want to to be
"Hello 2956687 this is my 23421 string and 328 i need to flip all 321"
I use this regular expression to get all numbers:
Regex nums = new Regex("\d+");
解决方案
var replacedString =
Regex.Replace(//finds all matches and replaces them
myString, //string we're working with
@"\d+", //the regular expression to match to do a replace
m => new string(m.Value.Reverse().ToArray())); //a Lambda expression which
//is cast to the MatchEvaluator delegate, so once the match is found, it
//is replaced with the output of this method.
这篇关于C#反转字符串中的所有号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文