C # Invert all numbers in a string?

I have a line:

"Hello 7866592 is my line 12432 and 823 I need to flip all 123 "

And I want to be

"Hello 2956687 is my line 23421 and 328 I need to flip all 321 "

I use this regex to get all numbers:

Regex nums = new Regex("\d+"); 
+4
source share
2 answers
 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. 
+17
source

Divide the line into spaces. Then grab the strings in the new string array, which are numbers, and run this function on them:

 public static string Reverse( string s ) { char[] charArray = s.ToCharArray(); Array.Reverse( charArray ); return new string( charArray ); } 

Then recombine the array into one line.

+1
source

All Articles