温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:MyShop网络商城源码(mvc开发)
当前文件路径:MyShop/BLL/Utils.cs

1using System; 2
using System.Collections.Generic; 3
using System.Text; 4
using System.Security.Cryptography; 5
using System.Text.RegularExpressions; 6
using System.Web; 7
using System.Web.UI; 8
using Microsoft.VisualBasic; 9
using System.IO; 10
using System.Security.Permissions; 11
using System.Collections; 12
using System.Runtime.InteropServices; 13
14
namespace MyShop.BLL 15
...{ 16
/**//// <summary> 17
/// 工具类 18
/// </summary> 19
public class Utils 20
...{ 21
String字符串类#region String字符串类 22
/**//// <summary> 23
/// 返回字符串真实长度, 1个汉字长度为2 24
/// </summary> 25
/// <returns></returns> 26
public static int GetStringLength(string str) 27
...{ 28
return Encoding.Default.GetBytes(str).Length; 29
} 30
/**//// <summary> 31
/// 过滤非法字符 32
/// </summary> 33
/// <param name="str"></param> 34
/// <returns></returns> 35
public static string ReplaceBadChar(string str) 36
...{ 37
if (string.IsNullOrEmpty(str)) 38
return ""; 39
string strBadChar, tempChar; 40
string[] arrBadChar; 41
strBadChar = "@@,+,',--,%,^,&,?,(,),<,>,[,],{,},/,\\,;,:,\",\"\","; 42
arrBadChar = SplitString(strBadChar, ","); 43
tempChar = str; 44
for (int i = 0; i < arrBadChar.Length; i++) 45
...{ 46
if (arrBadChar[i].Length > 0) 47
tempChar = tempChar.Replace(arrBadChar[i], ""); 48
} 49
return tempChar; 50
} 51
52
53
/**//// <summary> 54
/// 检查是否含有非法字符 55
/// </summary> 56
/// <param name="str">要检查的字符串</param> 57
/// <returns></returns> 58
public static bool ChkBadChar(string str) 59
...{ 60
bool result = false; 61
if (string.IsNullOrEmpty(str)) 62
return result; 63
string strBadChar, tempChar; 64
string[] arrBadChar; 65
strBadChar = "@@,+,',--,%,^,&,?,(,),<,>,[,],{,},/,\\,;,:,\",\"\""; 66
arrBadChar = SplitString(strBadChar, ","); 67
tempChar = str; 68
for (int i = 0; i < arrBadChar.Length; i++) 69
...{ 70
if (tempChar.IndexOf(arrBadChar[i]) >= 0) 71
result = true; 72
} 73
return result; 74
} 75
76
77
/**//// <summary> 78
/// 分割字符串 79
/// </summary> 80
public static string[] SplitString(string strContent, string strSplit) 81
...{ 82
int i = strContent.IndexOf(strSplit); 83
if (strContent.IndexOf(strSplit) < 0) 84
...{ 85
string[] tmp = ...{ strContent }; 86
return tmp; 87
} 88
//return Regex.Split(strContent, @strSplit.Replace(".", @"\."), RegexOptions.IgnoreCase); 89
90
return Regex.Split(strContent, @strSplit.Replace(".", @"\.")); 91
} 92
/**//// <summary> 93
/// 检测是否有危险的可能用于链接的字符串 94
/// </summary> 95
/// <param name="str">要判断字符串</param> 96
/// <returns>判断结果</returns> 97
public static bool IsSafeUserInfoString(string str) 98
...{ 99
return !Regex.IsMatch(str, @"/^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|$guestexp/is"); 100
} 101
102
/**//// <summary> 103
/// string型转换为int型 104
/// </summary> 105
/// <param name="strValue">要转换的字符串</param> 106
/// <returns>转换后的int类型结果.如果要转换的字符串是非数字,则返回-1.</returns> 107
public static int StrToInt(object strValue) 108
...{ 109
int defValue = -1; 110
if ((strValue == null) || (strValue.ToString() == string.Empty) || (strValue.ToString().Length > 10)) 111
...{ 112
return defValue; 113
} 114
115
string val = strValue.ToString(); 116
string firstletter = val[0].ToString(); 117
118
if (val.Length == 10 && IsNumber(firstletter) && int.Parse(firstletter) > 1) 119
...{ 120
return defValue; 121
} 122
else if (val.Length == 10 && !IsNumber(firstletter)) 123
...{ 124
return defValue; 125
} 126
127
128
int intValue = defValue; 129
if (strValue != null) 130
...{ 131
bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString()); 132
if (IsInt) 133
...{ 134
intValue = Convert.ToInt32(strValue); 135
} 136
} 137
138
return intValue; 139
} 140
141
/**//// <summary> 142
/// string型转换为int型 143
/// </summary> 144
/// <param name="strValue">要转换的字符串</param> 145
/// <param name="defValue">缺省值</param> 146
/// <returns>转换后的int类型结果</returns> 147
public static int StrToInt(object strValue, int defValue) 148
...{ 149
if ((strValue == null) || (strValue.ToString() == string.Empty) || (strValue.ToString().Length > 10)) 150
...{ 151
return defValue; 152
} 153
154
string val = strValue.ToString(); 155
string firstletter = val[0].ToString(); 156
157
if (val.Length == 10 && IsNumber(firstletter) && int.Parse(firstletter) > 1) 158
...{ 159
return defValue; 160
} 161
else if (val.Length == 10 && !IsNumber(firstletter)) 162
...{ 163
return defValue; 164
} 165
166
167
int intValue = defValue; 168
if (strValue != null) 169
...{ 170
bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString()); 171
if (IsInt) 172
...{ 173
intValue = Convert.ToInt32(strValue); 174
} 175
} 176
177
return intValue; 178
} 179
180
/**//// <summary> 181
/// string型转换为float型 182
/// </summary> 183
/// <param name="strValue">要转换的字符串</param> 184
/// <param name="defValue">缺省值</param> 185
/// <returns>转换后的int类型结果</returns> 186
public static float StrToFloat(object strValue, float defValue) 187
...{ 188
if ((strValue == null) || (strValue.ToString().Length > 10)) 189
...{ 190
return defValue; 191
} 192
193
float intValue = defValue; 194
if (strValue != null) 195
...{ 196
bool IsFloat = new Regex(@"^([-]|[0-9])[0-9]*(\.\w*)?$").IsMatch(strValue.ToString()); 197
if (IsFloat) 198
...{ 199
intValue = Convert.ToSingle(strValue); 200
} 201
} 202
return intValue; 203
} 204
205
206
/**//// <summary> 207
/// string型转换为时间型 208
/// </summary> 209
/// <param name="strValue">要转换的字符串</param> 210
/// <param name="defValue">缺省值</param> 211
/// <returns>转换后的时间类型结果</returns> 212
public static DateTime StrToDateTime(object strValue,DateTime defValue) 213
...{ 214
if ((strValue == null) || (strValue.ToString().Length > 20)) 215
...{ 216
return defValue; 217
} 218
219
DateTime intValue ; 220
221
if (!DateTime.TryParse(strValue.ToString(), out intValue)) 222
...{ 223
intValue = defValue; 224
} 225
return intValue; 226
} 227
228
229
/**//// <summary> 230
/// 判断给定的字符串(strNumber)是否是数值型 231
/// </summary> 232
/// <param name="strNumber">要确认的字符串</param> 233
/// <returns>是则返加true 不是则返回 false</returns> 234
public static bool IsNumber(string strNumber) 235
...{ 236
return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber); 237
} 238
239
240
/**//// <summary> 241
/// 检测是否符合email格式 242
/// </summary> 243
/// <param name="strEmail">要判断的email字符串</param> 244
/// <returns>判断结果</returns> 245
public static bool IsValidEmail(string strEmail) 246
...{ 247
return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 248
} 249
250
251
/**//// <summary> 252
/// 检测是否符合url格式,前面必需含有http:// 253
/// </summary> 254
/// <param name="url"></param> 255
/// <returns></returns> 256
public static bool IsURL(string url) 257
...{ 258
return Regex.IsMatch(url,@"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$"); 259
} 260
261
/**//// <summary> 262
/// 检测是否符合电话格式 263
/// </summary> 264
/// <param name="phoneNumber"></param> 265
/// <returns></returns> 266
public static bool IsPhoneNumber(string phoneNumber) 267
...{ 268
return Regex.IsMatch(phoneNumber,@"^(\(\d{3}\)|\d{3}-)




