-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
117 lines (110 loc) · 3.74 KB
/
Copy pathStringExtensions.cs
File metadata and controls
117 lines (110 loc) · 3.74 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
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
namespace ControlLibrary.Extensions
{
public static class StringExtensions
{
/// <summary>
/// method to strip HTML tags from a string
/// </summary>
/// <param name="str">the string to strip</param>
/// <param name="decodeFirst">if set to <c>true</c> [decode first].</param>
/// <returns></returns>
public static string StripHtml(this string str, bool decodeFirst = true)
{
try
{
while (((str.IndexOf("<") > -1) && (str.IndexOf(">") > -1) && (str.IndexOf("<") < str.IndexOf(">"))))
{
var start = str.IndexOf("<");
var end = str.IndexOf(">");
var count = end - start + 1;
str = str.Remove(start, count);
}
str = str.Replace(" ", " ");
str = str.Replace(">", "");
str = str.Replace("\r\n", "");
return str.Trim();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// string to MD5String
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ComputeMD5(this string str)
{
var alg = HashAlgorithmProvider.OpenAlgorithm("MD5");
IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
var hashed = alg.HashData(buff);
var res = CryptographicBuffer.EncodeToHexString(hashed);
return res;
}
/// <summary>
/// string to TimeString
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ConvertStringToTimeString(this string str)
{
try
{
DateTime time = Convert.ToDateTime(str, CultureInfo.InvariantCulture); // Converts only the time
string strTime = time.ToString("HH:mm:ss");
return strTime;
}
catch (Exception)
{
//第3个参数IFormatProvider provider 可以为null
return DateTime.ParseExact(str, "HH:mm:ss", CultureInfo.InvariantCulture).ToString();
}
}
/// <summary>
/// int to TimeString
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ConvertIntToTimeString(this string str)
{
string temp = string.Empty;
DateTime time = new DateTime().Add(TimeSpan.FromSeconds(System.Convert.ToInt32(str)));
if (System.Convert.ToInt32(str) >= 3600)
{
temp = time.ToString("HH:mm:ss");
}
else
{
temp = time.ToString("mm:ss");
}
return temp;
}
/// <summary>
/// 反转字符串
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string Reverse(this string s)
{
return new string(s.ReverseChar());
}
public static char[] ReverseChar(this string s)
{
return s.ToCharArray().Reverse().ToArray();
}
public static string Slice(this string source, int start)
{
return new string(source.ToCharArray().Slice(start));
}
}
}