forked from zhontai/Admin.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHelper.cs
More file actions
74 lines (70 loc) · 2.52 KB
/
StringHelper.cs
File metadata and controls
74 lines (70 loc) · 2.52 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
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace Admin.Core.Common.Helpers
{
/// <summary>
/// 字符串帮助类
/// </summary>
public class StringHelper
{
private static readonly char[] _constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
/// <summary>
/// 生成随机字符串,默认32位
/// </summary>
/// <param name="length">随机数长度</param>
/// <returns></returns>
public static string GenerateRandom(int length = 32)
{
var newRandom = new StringBuilder();
var rd = new Random();
for (int i = 0; i < length; i++)
{
newRandom.Append(_constant[rd.Next(_constant.Length)]);
}
return newRandom.ToString();
}
/// <summary>
/// 生成随机字符串,只包含数字
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GenerateRandomNumber(int length = 6)
{
var newRandom = new StringBuilder();
var rd = new Random();
for (int i = 0; i < length; i++)
{
newRandom.Append(_constant[rd.Next(10)]);
}
return newRandom.ToString();
}
public static string Format(string str, object obj)
{
if (str.IsNull())
{
return str;
}
string s = str;
if (obj.GetType().Name == "JObject")
{
foreach (var item in (Newtonsoft.Json.Linq.JObject)obj)
{
var k = item.Key.ToString();
var v = item.Value.ToString();
s = Regex.Replace(s, "\\{" + k + "\\}", v, RegexOptions.IgnoreCase);
}
}
else
{
foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties())
{
var xx = p.Name;
var yy = p.GetValue(obj).ToString();
s = Regex.Replace(s, "\\{" + xx + "\\}", yy, RegexOptions.IgnoreCase);
}
}
return s;
}
}
}