Advertisement
sylviapsh

Replace а href tags With URL tags

Jan 30th, 2013
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2.  
  3. class ReplaceAhrefWithURLTags
  4. {
  5.   //Write a program that replaces in a HTML document given as string all the tags <a href="…">…</a> with corresponding tags [URL=…]…/URL]. Sample HTML fragment:
  6.   //<p>Please visit <a href="http://academy.telerik. com">our site</a> to choose a training course. Also visit <a href="www.devbg.org">our forum</a> to discuss the courses.</p>
  7.   //<p>Please visit [URL=http://academy.telerik. com]our site[/URL] to choose a training course. Also visit [URL=www.devbg.org]our forum[/URL] to discuss the courses.</p>
  8.  
  9.   static void Main()
  10.   {
  11.     string[] findTags = { "<a href=\"", "\">", "</a>" }; //Strings to find
  12.     string[] replaceTags = { "[URL=", "]", "[/URL]"};//Replace strings
  13.  
  14.     string htmlText = @"<p>Please visit <a href=""http://academy.telerik. com"">our site</a> to choose a training course. Also visit <a href=""www.devbg.org"">our forum</a> to discuss the courses.</p>";//Html string to work with: @ to escape the // and double quotes ("") to escape the quotes (").
  15.  
  16.     for (int i = 0; i < findTags.Length; i++)
  17.             {
  18.         htmlText = htmlText.Replace(findTags[i], replaceTags[i]);//Do the replacements
  19.             }
  20.  
  21.     Console.WriteLine(htmlText);//Print the result
  22.   }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement