Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var text = "<i>Ivandro Ismael</i>";
- var stopWatch = System.Diagnostics.Stopwatch.StartNew();
- var first_result = string.Empty;
- for (int i = 0; i < 10000000; i++)
- {
- first_result = RemoveStartEndNoise1(text);
- if (i == 0)
- Console.Write("First result method 1: " + first_result);
- }
- stopWatch.Stop();
- Console.WriteLine(stopWatch.Elapsed);
- stopWatch.Restart();
- stopWatch.Start();
- for (int i = 0; i < 10000000; i++)
- {
- first_result = RemoveStartEndNoise2(text);
- if (i == 0)
- Console.Write("First result method 2: " + first_result);
- }
- stopWatch.Stop();
- Console.WriteLine(stopWatch.Elapsed);
- //var result = RemoveStartEndNoise1(text);
- //Console.WriteLine(result);
- Console.ReadKey();
- }
- public static string RemoveStartEndNoise1(string text)
- {
- const int i = ('i' | 'I');
- const int b = ('b' | 'B');
- const int u = ('u' | 'U');
- string s = text.Trim();
- // <i> <I> | <b> <B> | <u> <U>
- if (text.Length > 3 && text[0] == '<' && text[2] == '>' &&
- ((text[1] & i) == text[1] || (text[1] & b) == text[1] || (text[1] & u) == text[1]))
- {
- s = s.Substring(3).TrimStart();
- }
- // </i> </I> | </b> </B> | </u> </U>
- int aIdx = s.Length - 4;
- int bIdx = aIdx + 2;
- if (s.Length > 4 && s[aIdx + 1] == '/' && s[aIdx] == '<' && s[aIdx + 3] == '>' &&
- ((s[bIdx] & i) == s[bIdx] || (s[bIdx] & b) == s[bIdx] || (s[bIdx] & u) == s[bIdx]))
- s = s.Substring(0, s.Length - 4);
- if (s.Length > 1 && s[0] == '-')
- s = s.TrimStart('-');
- return s.Trim();
- }
- public static string RemoveStartEndNoise2(string text)
- {
- string s = text.Trim();
- if (s.StartsWith("<b>") && s.Length > 3)
- s = s.Substring(3);
- if (s.StartsWith("<i>") && s.Length > 3)
- s = s.Substring(3);
- if (s.StartsWith("<u>") && s.Length > 3)
- s = s.Substring(3);
- if (s.StartsWith("<B>") && s.Length > 3)
- s = s.Substring(3);
- if (s.StartsWith("<I>") && s.Length > 3)
- s = s.Substring(3);
- if (s.StartsWith("<U>") && s.Length > 3)
- s = s.Substring(3);
- if (s.EndsWith("</b>") && s.Length > 4)
- s = s.Substring(0, s.Length - 4);
- if (s.EndsWith("</i>") && s.Length > 4)
- s = s.Substring(0, s.Length - 4);
- if (s.EndsWith("</u>") && s.Length > 4)
- s = s.Substring(0, s.Length - 4);
- if (s.EndsWith("</B>") && s.Length > 4)
- s = s.Substring(0, s.Length - 4);
- if (s.EndsWith("</I>") && s.Length > 4)
- s = s.Substring(0, s.Length - 4);
- if (s.EndsWith("</U>") && s.Length > 4)
- s = s.Substring(0, s.Length - 4);
- if (s.StartsWith("-") && s.Length > 2)
- s = s.TrimStart('-');
- return s.Trim();
- }
- }
- }
- // Note: the first method is like 10 second faster than second one...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement