/
StringExtensions.cs
46 lines (37 loc) · 1.12 KB
/
StringExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Text.RegularExpressions;
namespace Blogifier.Shared.Extensions;
public static class StringExtensions
{
public static string Capitalize(this string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
char[] a = str.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source.IndexOf(toCheck, comp) >= 0;
}
public static string SanitizePath(this string str)
{
if (string.IsNullOrWhiteSpace(str))
return string.Empty;
str = str.Replace("%2E", ".").Replace("%2F", "/");
if (str.Contains("..") || str.Contains("//"))
throw new ApplicationException("Invalid directory path");
return str;
}
public static string RemoveScriptTags(this string str)
{
var scriptRegex = new Regex(@"<script[^>]*>[\s\S]*?</script>");
return scriptRegex.Replace(str, "");
}
public static string RemoveImgTags(this string str)
{
var scriptRegex = new Regex(@"<img[^>]*>[\s\S]*?>");
return scriptRegex.Replace(str, "");
}
}