-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
105 lines (87 loc) · 3.04 KB
/
StringExtensions.cs
File metadata and controls
105 lines (87 loc) · 3.04 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using ExpressBase.Common.Structures;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace ExpressBase.Common.Extensions
{
public static class StringExtensions
{
public static string SingleQuoted(this string str)
{
return "'" + str + "'";
}
public static string DoubleQuoted(this string str)
{
return '"' + str + '"';
}
public static string GraveAccentQuoted(this string str)
{
return '`' + str + '`';
}
public static string RemoveCR(this string str)
{
return str.Replace("\r\n", string.Empty).Replace("\n", string.Empty);
}
public static string ToMD5Hash(this string str)
{
var md5 = MD5.Create();
//compute hash from the bytes of text
byte[] result = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(str));
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
//change it into 2 hexadecimal digits for each byte
strBuilder.Append(result[i].ToString("x2"));
}
return strBuilder.ToString();
}
public static string ToBase64(this string plainText)
{
return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(plainText));
}
public static string FromBase64(this string base64EncodedData)
{
return System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(base64EncodedData));
}
public static string Truncate(this string str, int length)
{
if (length == 0 || str.Length <= length)
return str;
else
return str.Substring(0, length) + "...";
}
#region RefId related string operations
public static EbObjectType GetEbObjectType(this string RefId)
{
return EbObjectTypes.Get(Convert.ToInt32(RefId.Split("-")[2]));
}
public static int GetEbObjectId(this string RefId)
{
return Convert.ToInt32(RefId.Split("-")[3]);
}
public static int GetEbObjectVerionId(this string RefId)
{
return Convert.ToInt32(RefId.Split("-")[4]);
}
#endregion
public static TEnum ToEnum<TEnum>(this string value, bool ignoreCase = false) where TEnum : struct
{
TEnum tenumResult;
Enum.TryParse<TEnum>(value, ignoreCase, out tenumResult);
return tenumResult;
}
public static string RemoveSpecialCharacters(this string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
{
sb.Append(c);
}
}
return sb.ToString();
}
}
}