Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a string which may contain "title1" twice in it.

e.g.

server/api/shows?title1=its always sunny in philadelphia&title1=breaking bad ...

I need to change the second instance of the word "title1" to "title2"

I already know how to identify whether there ARE two instances of the string in the string.

int occCount = Regex.Matches(callingURL, "title1=").Count;

if (occCount > 1)
{
     //here's where I need to replace the second "title1" to "title2"
}

I know we can probably use Regex here but I'm not able to get the replace on the second instance. Can anyone give me a hand?

share|improve this question
4  
if there are 3 instances of the word title1 do you need to change the third one to title3? – Sam I am 7 hours ago
This sounds like a broader problem to me. – Dan Teesdale 7 hours ago
it will NEVER have more than 2 instances – Rj. 7 hours ago
1  
you know the old saying NEVER SAY NEVER – DJ KRAZE 7 hours ago
I have checks to ensure the string will never have more than 2 instances. the outer string gets built on user selections on the screen and i account for the correct selections... – Rj. 7 hours ago

5 Answers

up vote 7 down vote accepted

This will only replace the second instance of title1 (and any subsequent instances) after the first:

string output = Regex.Replace(input, @"(?<=title1.*)title1", "title2");

However, if there are more than 2 instances, it may not be what you want. It's a little crude, but you can do this to handle any number of occurrences:

int i = 1;
string output = Regex.Replace(input, @"title1", m => "title" + i++);
share|improve this answer
im testing this now – Rj. 7 hours ago
this did the trick. thank you sir! – Rj. 7 hours ago
pswg, I know I already accepted your answer but I just tried to do exactly the same thing but with the word "episodetitle1" and it's not giving me the same results. Care to give another hand? callingURL = Regex.Replace(callingURL, @"(?<=episodetitle1.*)episodetitle1", "episodetitle2"); – Rj. 6 hours ago
@Rj. What's the input string? – p.s.w.g 6 hours ago
something like this: server/api/Shows?title1=breaking%20bad&title2=two%20and%20a%20half%20men&episode‌​title1=pilot&episodetitle2=a%20big%20bag%20of%20dog – Rj. 6 hours ago
show 1 more comment

You could perhaps make use of a negative lookahead:

title1(?!.*title1)

And replace with title2.

See how it's working here.

share|improve this answer
what if it has a single title..you would replace wrong one – Anirudh 7 hours ago
@Anirudh The user already made sure with the if that there are two instances of title1. – Jerry 7 hours ago
he says it will never have more than 2 instances.. – Anirudh 7 hours ago
Um.... So what? – Jerry 7 hours ago
it can have a single occurance – Anirudh 6 hours ago

you can specify a count, and an index to start searching at

string str = @"server/api/shows?title1=its always sunny in philadelphia&title1=breaking bad ...";

Regex regex = new Regex(@"title1");
str = regex.Replace(str, "title2", 1, str.IndexOf("title1") + 6);
share|improve this answer
This is a good approach, but why would you use a regular expression in the last step? You could simply do a string.Replace. – p.s.w.g 6 hours ago
@p.s.w.g does string.Replace have a field specifying where you start the search? – Sam I am 6 hours ago
Nope, you're right. You'd have to do a couple of string.Substring's first (like a one answer had, which is now deleted). – p.s.w.g 6 hours ago

I found this link immediately on a google search.

C# - indexOf the nth occurrence of a string?

Get the IndexOf the first occurrence of the string.

Use the returned IndexOf's startIndex +1 for the starting position of the second IndexOf.

Substring it into two strings at the appropriate index of the "1" character.

Concat it back together with the "2" character.

share|improve this answer

The way P.S.W.G did is really awesome. but below I mentioned one simple way to get it done for those who do have problem in lambda and regex expression..;)

int index = input.LastIndexOf("title1=");

string output4 = input.Substring(0, index - 1) + "&title2" + input.Substring(index + "title1".Length, input.Length - index - "title1".Length);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.