Reputation: 19047
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+");
Upvotes: 6
Views: 2211
Reputation: 68687
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.
Upvotes: 21
Reputation: 6549
Split the string on spaces. Then take the strings in the new string array that are digits 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 your array into a single string.
Upvotes: 1