我有一个字符串:
“你好7866592这是我的12432字符串和823我需要翻转所有123 ”
我想成为
“你好2956687这是我的23421字符串和328我需要翻转所有321 ”
我使用这个正则表达式来获取所有数字:
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.
在空格上拆分字符串。然后获取新字符串数组中的数字字符串并在它们上运行此函数:
public static string Reverse( string s )
{
char[] charArray = s.ToCharArray();
Array.Reverse( charArray );
return new string( charArray );
}
然后将您的数组重新组合成一个字符串。