Advertisement
ge_or_gi

Untitled

Feb 1st, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace NoHTMLtags
  10. {
  11. class NoHTMLtags
  12. {
  13.  
  14. // с любезното съдействие на http://www.dotnetperls.com/remove-html-tags
  15.  
  16. /// <summary>
  17. /// Remove HTML tags from string using char array.
  18. /// </summary>
  19. public static string StripTagsCharArray(string source)
  20. {
  21. char[] array = new char[source.Length];
  22. int arrayIndex = 0;
  23. bool inside = false;
  24.  
  25. for (int i = 0; i < source.Length; i++)
  26. {
  27. char let = source[i];
  28. if (let == '<')
  29. {
  30. inside = true;
  31. continue;
  32. }
  33. if (let == '>')
  34. {
  35. inside = false;
  36. continue;
  37. }
  38. if (!inside)
  39. {
  40. array[arrayIndex] = let;
  41. arrayIndex++;
  42. }
  43. }
  44. return new string(array, 0, arrayIndex);
  45. }
  46.  
  47.  
  48. static void Main(string[] args)
  49. {
  50.  
  51.  
  52. WebClient webCilent = new WebClient();
  53.  
  54. webCilent.Encoding = System.Text.Encoding.UTF8; //указва форматирането на текста - без него излизат маймуници особенно на кирилица
  55.  
  56. string sorceStr = webCilent.DownloadString("http://forums.academy.telerik.com/54788/c%23-%D0%B4%D0%BE%D0%BC%D0%B0%D1%88%D0%BD%D0%BE-strings-and-text-processing-25-%D0%B7%D0%B0%D0%B4%D0%B0%D1%87%D0%B0");
  57.  
  58. string stripSourceStr = StripTagsCharArray(sorceStr).Trim();
  59.  
  60.  
  61.  
  62. Dictionary<string, int> dictionary = new Dictionary<string, int>();
  63.  
  64. StringBuilder wordStrTotal = new StringBuilder();
  65.  
  66.  
  67. foreach (var word in Regex.Matches(stripSourceStr, @"\w+"))
  68. {
  69.  
  70. string wordStr = Convert.ToString(word);
  71.  
  72. wordStrTotal.Append(wordStr + ' ');
  73.  
  74. #region Проверява дължината на думат, проверява дали съществува и я добавя
  75. //if (wordStr.Length > 0)
  76. //{
  77. // if (dictionary.ContainsKey(wordStr))
  78. // {
  79. // dictionary[wordStr]++;
  80. // }
  81. // else
  82. // {
  83. // dictionary.Add(wordStr, 1);
  84. // }
  85. //}
  86. #endregion
  87. }
  88. #region Принтира на екрана думите който са в речника
  89. //foreach (KeyValuePair<string, int> item in dictionary)
  90. //{
  91. // Console.WriteLine("{0,15} -> {1}", item.Key, item.Value);
  92. //}
  93. #endregion
  94.  
  95. Console.ReadLine();
  96.  
  97. #region Принтира Съдържанието на СтрингБуилдера
  98. Console.WriteLine(wordStrTotal.ToString());
  99. #endregion
  100.  
  101.  
  102.  
  103.  
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement