1- using System ;
2- using System . Globalization ;
3- using System . Linq ;
4- using System . Net ;
5- using System . Security . Cryptography ;
6- using System . Text ;
7- using System . Text . RegularExpressions ;
8- using System . Threading ;
1+ // --------------------------------------------------------------------------------------------------------------------
2+ // <copyright file="StringHelper.cs" company="Abhith">
3+ // All rights reserved
4+ // </copyright>
5+ // <summary>
6+ // Defines the StringHelper type.
7+ // </summary>
8+ // --------------------------------------------------------------------------------------------------------------------
99
1010namespace Code . Library
1111{
12+ using System ;
13+ using System . Globalization ;
14+ using System . Linq ;
15+ using System . Net ;
16+ using System . Security . Cryptography ;
17+ using System . Text ;
18+ using System . Text . RegularExpressions ;
19+ using System . Threading ;
20+
1221 public static class StringHelper
1322 {
1423 private static MD5CryptoServiceProvider s_md5 = null ;
@@ -82,37 +91,28 @@ public static string FormatDates(DateTime? startDate, DateTime? endDate)
8291 }
8392
8493 /// <summary>
85- /// The regex strip html.
86- /// Reference : Gist
94+ /// Clean the content by replacing special characters/HTML tags.
8795 /// </summary>
88- private static readonly Regex RegexStripHtml = new Regex ( "<[^>]*>" , RegexOptions . Compiled ) ;
89-
90- private static string StripHtml ( string html )
91- {
92- return string . IsNullOrWhiteSpace ( html ) ? string . Empty :
93- RegexStripHtml . Replace ( html , string . Empty ) . Trim ( ) ;
94- }
95-
96- public static string CleanContent ( string content , bool removeHtml )
96+ /// <param name="content">
97+ /// The content.
98+ /// </param>
99+ /// <param name="removeHtml">
100+ /// The remove html.
101+ /// </param>
102+ /// <returns>
103+ /// The <see cref="string"/>.
104+ /// </returns>
105+ public static string Clean ( this string content , bool removeHtml )
97106 {
98107 if ( removeHtml )
99108 {
100109 content = StripHtml ( content ) ;
101110 }
102111
103- content =
104- content . Replace ( "\\ " , string . Empty ) .
105- Replace ( "|" , string . Empty ) .
106- Replace ( "(" , string . Empty ) .
107- Replace ( ")" , string . Empty ) .
108- Replace ( "[" , string . Empty ) .
109- Replace ( "]" , string . Empty ) .
110- Replace ( "*" , string . Empty ) .
111- Replace ( "?" , string . Empty ) .
112- Replace ( "}" , string . Empty ) .
113- Replace ( "{" , string . Empty ) .
114- Replace ( "^" , string . Empty ) .
115- Replace ( "+" , string . Empty ) ;
112+ content = content . Replace ( "\\ " , string . Empty ) . Replace ( "|" , string . Empty ) . Replace ( "(" , string . Empty )
113+ . Replace ( ")" , string . Empty ) . Replace ( "[" , string . Empty ) . Replace ( "]" , string . Empty )
114+ . Replace ( "*" , string . Empty ) . Replace ( "?" , string . Empty ) . Replace ( "}" , string . Empty )
115+ . Replace ( "{" , string . Empty ) . Replace ( "^" , string . Empty ) . Replace ( "+" , string . Empty ) ;
116116
117117 var words = content . Split ( new [ ] { ' ' , '\n ' , '\r ' } , StringSplitOptions . RemoveEmptyEntries ) ;
118118 var sb = new StringBuilder ( ) ;
@@ -122,7 +122,7 @@ public static string CleanContent(string content, bool removeHtml)
122122 sb . AppendFormat ( "{0} " , word ) ;
123123 }
124124
125- return sb . ToString ( ) ;
125+ return sb . ToString ( ) . Trim ( ) ;
126126 }
127127
128128 /// <summary>
@@ -386,5 +386,17 @@ public static string ReplaceLastOccurrence(this string source, string find, stri
386386 }
387387
388388 #endregion extension methods
389+
390+ /// <summary>
391+ /// The regex strip html.
392+ /// Reference : Gist
393+ /// </summary>
394+ private static readonly Regex RegexStripHtml = new Regex ( "<[^>]*>" , RegexOptions . Compiled ) ;
395+
396+ private static string StripHtml ( string html )
397+ {
398+ return string . IsNullOrWhiteSpace ( html ) ? string . Empty :
399+ RegexStripHtml . Replace ( html , string . Empty ) . Trim ( ) ;
400+ }
389401 }
390402}
0 commit comments