Answered by:
How to Replace specific word in given string.

Question
-
User-919499280 posted
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.
Friday, July 25, 2014 6:57 AM
Answers
-
User1508394307 posted
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
www.google.com(?![/\w])
Complete example: https://dotnetfiddle.net/HJxYVQ
Output:
http://www.google.com?newstring <br/> http://www.google.com/services/ <br/> http://www.google.com?newstring
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 25, 2014 9:52 AM
All replies
-
User1283497924 posted
For replacing specific work from a string you can use Replace function present in .net.
plz check eg;
string str="This is really new place."; string newstr=str.Replace("new","old");
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
Friday, July 25, 2014 7:04 AM -
User1508394307 posted
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;
Friday, July 25, 2014 7:08 AM -
User-919499280 posted
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.
string newstr=str.Replace("are","is");
Result: Thare are really new place.
but i want below result.
This are really new place.
Friday, July 25, 2014 7:15 AM -
User-919499280 posted
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.
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.
Friday, July 25, 2014 7:26 AM -
User1508394307 posted
string newstr=str.Replace("are","is");
Result: Thare are really new place.
but i want below result.
This are really new place.
In this case you should use
string newstr=str.Replace("Thare","This");
Friday, July 25, 2014 7:28 AM -
User-919499280 posted
@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 2<sup>nd</sup> 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 1<sup>st</sup> link only. I want to replace specific word.
Friday, July 25, 2014 7:39 AM -
User1508394307 posted
Why not simply do like this?
string search = "http://www.google.com";
string replace = "http://www.google.com?ClientTags"; int pos = text.IndexOf(search); if (pos != -1) text = text.Substring(0, pos) + replace + text.Substring(pos + search.Length);Friday, July 25, 2014 7:54 AM -
User-919499280 posted
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";
string search = "http://www.google.com";
string replace = "http://www.google.com?ClientTags"; int pos = text.IndexOf(search); if (pos != -1) text = text.Substring(0, pos) + replace + text.Substring(pos + search.Length);Friday, July 25, 2014 8:56 AM -
User1508394307 posted
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
www.google.com(?![/\w])
Complete example: https://dotnetfiddle.net/HJxYVQ
Output:
http://www.google.com?newstring <br/> http://www.google.com/services/ <br/> http://www.google.com?newstring
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 25, 2014 9:52 AM -
User-919499280 posted
Thank you very much smirov. Can you please given some reference site address of how to write above patterns.
Saturday, July 26, 2014 1:16 AM -
User-919499280 posted
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.
Saturday, July 26, 2014 4:50 AM -
User1508394307 posted
If http://www.google.com?UTM_Source=test should not be replaced then add ? in [ ]
www.google.com(?![?/\w])
Saturday, July 26, 2014 6:41 AM