我需要反转文本中的所有数字组,例如
"test text 145 for 23 site 1"
我们需要这个输出
"test text 541 fro 32 site 1"
答案 0 :(得分:8)
C# Reverse all numbers in string?
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.
答案 1 :(得分:0)
我会做这样的事情......虽然没有经过测试
public string reverseString(string valuetoReverse)
{
StringBuilder s = new StringBuilder();
List<string> newString = valuetoReverse.Split(' ').Reverse().ToList();
newString.ForEach(v => s.Append(v + String.Empty));
return s.ToString();
}