SilverLight
SilverLight

Reputation: 20468

How replace unique string include some characters Before with an increment Number

Here is my string :

...
...
...8451__Same_String__...
...5236__Same_String__...
...9854__Same_String__...
...8751__Same_String__...
...3254__Same_String__...
...
...

Dots mean -> other characters in my string.
As you see there are 5 same SubStrings in my string that i want to change 4 digits number before all of those same SubStrings with an increment number!
Mean after changes (using with remove or replace or regex or whatever) i want this string :

...
...
...1111__Same_String__...
...2222__Same_String__...
...3333__Same_String__...
...4444__Same_String__...
...5555__Same_String__...
...
...

As you see Same_String is not my goal and my goal is those 4 digits numbers that should change like this : 1111 ,2222 ,3333 ,4444 ,5555 ,...

How can i do that?

Upvotes: 1

Views: 176

Answers (1)

EZI
EZI

Reputation: 15364

You can use Regex,

int count = 0;
var result = Regex.Replace(
               text, 
               @"\d\d\d\d(__Same_String__)", 
               m => (++count).ToString().PadLeft(4, (char)(count + '0')) + m.Groups[1].Value);

Upvotes: 3

Related Questions