反转字符串中的任何数字组

时间:2014-01-05 07:49:29

标签: c# string reverse

我需要反转文本中的所有数字组,例如

"test text 145 for 23 site 1"

我们需要这个输出

"test text 541 fro 32 site 1"

2 个答案:

答案 0 :(得分:8)

C# Reverse all numbers in string?

感谢Yuriy Faktorovich

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();
}