Advertisement
csaki

Remove HTML tags from .csv files

Jul 4th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5.  
  6. namespace HTMLTagRemove
  7. {
  8.     internal class Program
  9.     {
  10.         private const string INPUT_FILENAME = "termekek.csv";
  11.         private const string OUTPUT_FILENAME = "termekek_result.csv";
  12.  
  13.         private static void Main()
  14.         {
  15.             string[] rows;
  16.             using (var reader = new StreamReader(INPUT_FILENAME, Encoding.Default))
  17.             {
  18.                 rows = reader.ReadToEnd().Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
  19.             }
  20.             rows = RemoveHtmlTags(rows);
  21.             using (var writer = new StreamWriter(OUTPUT_FILENAME, false, Encoding.UTF8))
  22.             {
  23.                 writer.Write(string.Join(";", rows));
  24.             }
  25.             Process.Start(OUTPUT_FILENAME);
  26.         }
  27.  
  28.         private static string[] RemoveHtmlTags(string[] strings)
  29.         {
  30.             for (int i = 0; i < strings.Length; i++)
  31.             {
  32.                 strings[i] = RemoveTagInString(strings[i]);
  33.             }
  34.             return strings;
  35.         }
  36.  
  37.         public static string RemoveTagInString(string sourceString)
  38.         {
  39.             var charray = new char[sourceString.Length];
  40.             int index = 0;
  41.             bool isInside = false;
  42.             foreach (char character in sourceString)
  43.             {
  44.                 if (character == '<')
  45.                 {
  46.                     isInside = true;
  47.                     continue;
  48.                 }
  49.                 if (character == '>')
  50.                 {
  51.                     isInside = false;
  52.                     continue;
  53.                 }
  54.                 if (isInside) continue;
  55.  
  56.                 charray[index] = character;
  57.                 index++;
  58.             }
  59.             return new string(charray, 0, index);
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement