CodeMonkey
CodeMonkey

Reputation: 12424

C# replace a certain type of character in a string

Let's say I've got a string with the value of "C4". In my code I generate some number, let's say it's 200, and now I want my string to look like this: "C200". Of course that nothing is known at compile time.

Is it possible to do this change with a single elegant line? I know this line I will write is not syntactically correct but this is the idea I'm looking for:

newString = oldString.Replace(typeof (int), newNumber.ToString());

Upvotes: 1

Views: 117

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

You can use regular expression:

newString = Regex.Replace(oldString, @"\d+", newNumber.ToString());

Upvotes: 6

Related Questions