Skip to content

Commit bb7d51c

Browse files
authored
Merge pull request #22 from Abhith/dev
Clean content method to extension method Clean
2 parents 0d78311 + dd8191c commit bb7d51c

File tree

4 files changed

+83
-35
lines changed

4 files changed

+83
-35
lines changed

Source/Code.Library/Code.Library.Tests/StringHelperTests.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using Code.Library;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
23
using System;
34
using System.Globalization;
45
using System.Threading;
56

67
namespace Code.Library.Tests
78
{
9+
using Shouldly;
10+
11+
using Xunit;
12+
13+
using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
14+
815
[TestClass()]
916
public class StringHelperTests
1017
{
@@ -115,5 +122,24 @@ public void UrlAvailableTest()
115122
}
116123

117124
#endregion Public Methods
125+
126+
/// <summary>
127+
/// The clean content test.
128+
/// </summary>
129+
[Fact]
130+
public void CleanTest()
131+
{
132+
var input = "$7.42 billion";
133+
134+
var output = input.Clean(true);
135+
136+
output.ShouldBe(input);
137+
138+
input = "SELECT accountNumber, balance FROM accounts WHERE account_owner_id = 0 OR 1=1";
139+
140+
output = input.Clean(true);
141+
142+
output.ShouldNotBe(input);
143+
}
118144
}
119145
}

Source/Code.Library/Code.Library.sln

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.10
4+
VisualStudioVersion = 15.0.27004.2006
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Code.Library", "Code.Library\Code.Library.csproj", "{960C56C3-AC2C-4AC4-B0CC-7DFE9A4EA3CA}"
77
EndProject
@@ -25,4 +25,7 @@ Global
2525
GlobalSection(SolutionProperties) = preSolution
2626
HideSolutionNode = FALSE
2727
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {FCC7EF98-013D-45AB-8F75-6B723ADAE231}
30+
EndGlobalSection
2831
EndGlobal

Source/Code.Library/Code.Library/Code.Library.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
<ErrorReport>prompt</ErrorReport>
3030
<WarningLevel>4</WarningLevel>
3131
</PropertyGroup>
32+
<PropertyGroup>
33+
<SignAssembly>true</SignAssembly>
34+
</PropertyGroup>
35+
<PropertyGroup>
36+
<AssemblyOriginatorKeyFile>code.lib.key.pfx</AssemblyOriginatorKeyFile>
37+
</PropertyGroup>
3238
<ItemGroup>
3339
<Reference Include="System" />
3440
<Reference Include="System.Core" />
@@ -61,6 +67,7 @@
6167
<Compile Include="Helpers\ValidationHelper.cs" />
6268
</ItemGroup>
6369
<ItemGroup>
70+
<None Include="code.lib.key.pfx" />
6471
<None Include="packages.config" />
6572
</ItemGroup>
6673
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

Source/Code.Library/Code.Library/Helpers/StringHelper.cs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
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

1010
namespace 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

Comments
 (0)