-5

Example: If there is a line http://google.com/adi/727412;sz=728x90;ord=$RANDOM? which contains adi in it, wants it to be replaced with http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM? and rest all other text to be same with no change.

Please help

2 Answers2

0
        string url = "http://google.com/adi/727412;sz=728x90;ord=$RANDOM?";
        if(url.Contains("adi")) url = "http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM?";


        string url = "blablablablablahttp://google.com/adi/727412;sz=728x90;ord=$RANDOM?blablabla";
        if(url.Contains("adi")) url.Replace("http://google.com/adi/727412;sz=728x90;ord=$RANDOM?", "http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM?");
Mikatsu
  • 530
  • 2
  • 4
  • 15
0

This is a fairly simple task:

string url = @"http://google.com/adi/727412;sz=728x90;ord=$RANDOM?";

if (url.Contains(@"/adi/"))
{
    int pos = url.IndexOf(";ord"); //// Find first occurence of Ord parameter
    url = url.Insert(pos, ";click=$CLICK"); //// Insert text at position
}

Edit: To accomplish the task for multiple occurences I used a solution from this thread.

{
    string url = "<google.com/adi/727412;sz=728x90;ord=$RANDOM?>; <google.com/adi/727412;sz=300x250;ord=$RANDOM?>";
    string searchString = @"/adi/";

    int n = 0;

    while ((n = url.IndexOf(searchString, n)) != -1)
    {
        n += searchString.Length;
        int pos = url.IndexOf('?', n);
        url = url.Insert(pos, ";click=$CLICK");
    }
}
Community
  • 1
  • 1
Alina B.
  • 1,256
  • 8
  • 18