Actually my requirement is i have one template, It having lot of Links. I want to add tags(Google analytic tags) to end of each and every link that why i maintain all links in DataTable then looping it and changing one by one. It is fine but in my template
having two links like below.
Thanks for reply here the brief explanation of my actual problem.
Actually my requirement is I have one template with lot of links. I get all links in one DataTable. I am looping that DataTable and replace each every link with Google analytics tag which is given by my client.
Here problem is in my template having two links like below.
Thanks for reply. It is working only when text contain one http://www.google.com working fine. But if your template having multiple
http://www.google.com links above logic not working. For example like below.
text =@"http://www.google.com <br/> http://www.google.com/services/ <br/> http://www.google.com";
First of all it is not clear why if you have all urls in the database you put them all in a string and then replace in that string. Instead you can do a loop through all rows and compare urls one by one.
If this is not possible for some reasons then you can try
Member
5 Points
40 Posts
How to Replace specific word in given string.
Jul 25, 2014 06:57 AM|swaroopkumar1069|LINK
Hi,
I want to replace specific word in below given string. Please look out below small code.
DataTable dtURLs = new DataTable();
DataColumn s1 = new DataColumn("OldURL");
dtURLs.Columns.Add(s1);
DataRow row1 = dtURLs.NewRow();
row1[s1] = "https://www.google.com";
dtURLs.Rows.Add(row1);
DataRow row2 = dtURLs.NewRow();
row2[s1] = "https://www.google.com/services/";
dtURLs.Rows.Add(row2);
string campaignContent =
@"https://www.google.com <br/> https://www.google.com/services/";
string queryString = "queryString";
foreach (DataRow dr in dtURLs.Rows)
{
string pattern = @String.Format(@"\b{0}\b", dr["OldURL"].ToString());
campaignContent =
Regex.Replace(campaignContent, pattern, dr["OldURL"].ToString() + "?" + queryString);
}
I am trying above the model but I got wrong output like below.
OUTPUT:
https://www.google.com?queryString <br/> https://www.google.com?queryString /services/
But I am excepted OUTPUT is like below.
https://www.google.com?queryString <br/> https://www.google.com/services/?queryString
Please give any solutions for above issue.
C#.net
Contributor
5745 Points
1899 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 07:04 AM|vinay13mar|LINK
For replacing specific work from a string you can use Replace function present in .net.
plz check eg;
in above code we are replacing new word with old present in string
http://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx
C#.net
V.K.Singh
All-Star
35159 Points
9075 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 07:08 AM|smirnov|LINK
Why not simply to add query string at the end?
//string pattern = @String.Format(@"\b{0}\b", dr["OldURL"].ToString());
//campaignContent = Regex.Replace(campaignContent, pattern, dr["OldURL"].ToString() + "?" + queryString);
campaignContent = dr["OldURL"].ToString() + "?" + queryString;
C#.net
Member
5 Points
40 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 07:15 AM|swaroopkumar1069|LINK
Hi vinay,
i know above replace function. I want specific word for example if your word having 'is' two times right?
I want to replace 'is' word only in your sentence. U got my point.
here output is like below.
Result: Thare are really new place.
but i want below result.
This are really new place.
C#.net
Member
5 Points
40 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 07:26 AM|swaroopkumar1069|LINK
HI Smirnov,
Actually my requirement is i have one template, It having lot of Links. I want to add tags(Google analytic tags) to end of each and every link that why i maintain all links in DataTable then looping it and changing one by one. It is fine but in my template having two links like below.
1. http://www.google.com
2. http://www.google.com/service.
I want to change only http://www.google.com to http://www.google.com?queryStirng don't disturb other links.
C#.net
All-Star
35159 Points
9075 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 07:28 AM|smirnov|LINK
In this case you should use
C#.net
Member
5 Points
40 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 07:39 AM|swaroopkumar1069|LINK
@vinay & @smirnov
Thanks for reply here the brief explanation of my actual problem.
Actually my requirement is I have one template with lot of links. I get all links in one DataTable. I am looping that DataTable and replace each every link with Google analytics tag which is given by my client.
Here problem is in my template having two links like below.
I want to replace “ http://www.google.com “ to “ http://www.google.com?ClientTags “ don’t disturb other links. My problem is while replacing 2nd link also replaced I am getting result like below
http://www.google.com?ClientTags …… something matter …. http://www.google.com?ClientTags/Services
I want to replace only 1st link only. I want to replace specific word.
C#.net
All-Star
35159 Points
9075 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 07:54 AM|smirnov|LINK
Why not simply do like this?
C#.net
Member
5 Points
40 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 08:56 AM|swaroopkumar1069|LINK
Thanks for reply. It is working only when text contain one http://www.google.com working fine. But if your template having multiple http://www.google.com links above logic not working. For example like below.
text =@"http://www.google.com <br/> http://www.google.com/services/ <br/> http://www.google.com";
C#.net
All-Star
35159 Points
9075 Posts
Re: How to Replace specific word in given string.
Jul 25, 2014 09:52 AM|smirnov|LINK
First of all it is not clear why if you have all urls in the database you put them all in a string and then replace in that string. Instead you can do a loop through all rows and compare urls one by one.
If this is not possible for some reasons then you can try
Complete example: https://dotnetfiddle.net/HJxYVQ
Output:
C#.net
Member
5 Points
40 Posts
Re: How to Replace specific word in given string.
Jul 26, 2014 01:16 AM|swaroopkumar1069|LINK
Thank you very much smirov. Can you please given some reference site address of how to write above patterns.
C#.net
Member
5 Points
40 Posts
Re: How to Replace specific word in given string.
Jul 26, 2014 04:50 AM|swaroopkumar1069|LINK
The above solution working fine. IF links having already parameters at that time above code not working. plz check below link.
https://dotnetfiddle.net/h6YsnP
http://www.google.com?newstring?UTM_Source=test This is wrong replacement. If Content links having with parameter and with out parameters.
C#.net
All-Star
35159 Points
9075 Posts
Re: How to Replace specific word in given string.
Jul 26, 2014 06:41 AM|smirnov|LINK
If http://www.google.com?UTM_Source=test should not be replaced then add ? in [ ]
C#.net