温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:某贸易公司项目管理系统源码
当前文件:
ProjectEva/App_Code/Common/StringUtil.cs[13K,2009-6-12 11:52:27],打开代码结构图
ProjectEva/App_Code/Common/StringUtil.cs[13K,2009-6-12 11:52:27],打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Web.UI.WebControls.WebParts; 9
using System.Web.UI.HtmlControls; 10
using System.IO; 11
using System.Web.SessionState; 12
using System.Xml; 13
using System.Security; 14
using System.Net; 15
using System.Text.RegularExpressions; 16
/// <summary> 17
/// 黄建华(2007.9) 18
/// 字符串的处理,字符的转换 19
/// </summary> 20
public class StringUtil 21
{ 22
public StringUtil() 23
{ 24
// 25
// TODO: 在此处添加构造函数逻辑 26
// 27
} 28
/// <summary> 29
/// 从字符串中的尾部删除指定的字符串 30
/// </summary> 31
/// <param name="sourceString"></param> 32
/// <param name="removedString"></param> 33
/// <returns></returns> 34
public static string Remove(string sourceString, string removedString) 35
{ 36
try 37
{ 38
if (sourceString.IndexOf(removedString) < 0) 39
throw new Exception("原字符串中不包含移除字符串!"); 40
string result = sourceString; 41
int lengthOfSourceString = sourceString.Length; 42
int lengthOfRemovedString = removedString.Length; 43
int startIndex = lengthOfSourceString - lengthOfRemovedString; 44
string tempSubString = sourceString.Substring(startIndex); 45
if (tempSubString.ToUpper() == removedString.ToUpper()) 46
{ 47
result = sourceString.Remove(startIndex, lengthOfRemovedString); 48
} 49
return result; 50
} 51
catch 52
{ 53
return sourceString; 54
} 55
} 56
57
/// <summary> 58
/// 获取拆分符右边的字符串 59
/// </summary> 60
/// <param name="sourceString"></param> 61
/// <param name="splitChar"></param> 62
/// <returns></returns> 63
public static string RightSplit(string sourceString, char splitChar) 64
{ 65
string result = null; 66
string[] tempString = sourceString.Split(splitChar); 67
if (tempString.Length > 0) 68
{ 69
result = tempString[tempString.Length - 1].ToString(); 70
} 71
return result; 72
} 73
74
/// <summary> 75
/// 获取拆分符左边的字符串 76
/// </summary> 77
/// <param name="sourceString"></param> 78
/// <param name="splitChar"></param> 79
/// <returns></returns> 80
public static string LeftSplit(string sourceString, char splitChar) 81
{ 82
string result = null; 83
string[] tempString = sourceString.Split(splitChar); 84
if (tempString.Length > 0) 85
{ 86
result = tempString[0].ToString(); 87
} 88
return result; 89
} 90
91
/// <summary> 92
/// 去掉最后一个逗号 93
/// </summary> 94
/// <param name="origin"></param> 95
/// <returns></returns> 96
public static string DelLastComma(string origin) 97
{ 98
if (origin.IndexOf(",") == -1) 99
{ 100
return origin; 101
} 102
return origin.Substring(0, origin.LastIndexOf(",")); 103
} 104
105
/// <summary> 106
/// 删除不可见字符 107
/// </summary> 108
/// <param name="sourceString"></param> 109
/// <returns></returns> 110
public static string DeleteUnVisibleChar(string sourceString) 111
{ 112
System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131); 113
for (int i = 0; i < sourceString.Length; i++) 114
{ 115
int Unicode = sourceString[i]; 116
if (Unicode >= 16) 117
{ 118
sBuilder.Append(sourceString[i].ToString()); 119
} 120
} 121
return sBuilder.ToString(); 122
} 123
124
/// <summary> 125
/// 获取数组元素的合并字符串 126
/// </summary> 127
/// <param name="stringArray"></param> 128
/// <returns></returns> 129
public static string GetArrayString(string[] stringArray) 130
{ 131
string totalString = null; 132
for (int i = 0; i < stringArray.Length; i++) 133
{ 134
totalString = totalString + stringArray[i]; 135
} 136
return totalString; 137
} 138
139
/// <summary> 140
/// 获取某一字符串在字符串数组中出现的次数 141
/// </summary> 142
/// <param name="stringArray" type="string[]"> 143
/// <para> 144
/// 145
/// </para> 146
/// </param> 147
/// <param name="findString" type="string"> 148
/// <para> 149
/// 150
/// </para> 151
/// </param> 152
/// <returns> 153
/// A int value... 154
/// </returns> 155
public static int GetStringCount(string[] stringArray, string findString) 156
{ 157
int count = -1; 158
string totalString = GetArrayString(stringArray); 159
string subString = totalString; 160
161
while (subString.IndexOf(findString) >= 0) 162
{ 163
subString = totalString.Substring(subString.IndexOf(findString)); 164
count += 1; 165
} 166
return count; 167
} 168
169
/// <summary> 170
/// 获取某一字符串在字符串中出现的次数 171
/// </summary> 172
/// <param name="stringArray" type="string"> 173
/// <para> 174
/// 原字符串 175
/// </para> 176
/// </param> 177
/// <param name="findString" type="string"> 178
/// <para> 179
/// 匹配字符串 180
/// </para> 181
/// </param> 182
/// <returns> 183
/// 匹配字符串数量 184
/// </returns> 185
public static int GetStringCount(string sourceString, string findString) 186
{ 187
int count = 0; 188
int findStringLength = findString.Length; 189
string subString = sourceString; 190
191
while (subString.IndexOf(findString) >= 0) 192
{ 193
subString = subString.Substring(subString.IndexOf(findString) + findStringLength); 194
count += 1; 195
} 196
return count; 197
} 198
199
/// <summary> 200
/// 截取从startString开始到原字符串结尾的所有字符 201
/// </summary> 202
/// <param name="sourceString" type="string"> 203
/// <para> 204
/// 205
/// </para> 206
/// </param> 207
/// <param name="startString" type="string"> 208
/// <para> 209
/// 210
/// </para> 211
/// </param> 212
/// <returns> 213
/// A string value... 214
/// </returns> 215
public static string GetSubString(string sourceString, string startString) 216
{ 217
try 218
{ 219
int index = sourceString.ToUpper().IndexOf(startString); 220
if (index > 0) 221
{ 222
return sourceString.Substring(index); 223
} 224
return sourceString; 225
} 226
catch 227
{ 228
return ""; 229
} 230
} 231
232
public static string GetSubString(string sourceString, string beginRemovedString, string endRemovedString) 233
{ 234
try 235
{ 236
if (sourceString.IndexOf(beginRemovedString) != 0) 237
beginRemovedString = ""; 238
239
if (sourceString.LastIndexOf(endRemovedString, sourceString.Length - endRemovedString.Length) < 0) 240
endRemovedString = ""; 241
242
int startIndex = beginRemovedString.Length; 243
int length = sourceString.Length - beginRemovedString.Length - endRemovedString.Length; 244
if (length > 0) 245
{ 246
return sourceString.Substring(startIndex, length); 247
} 248
return sourceString; 249
} 250
catch 251
{ 252
return sourceString; ; 253
} 254
} 255
256
/// <summary> 257
/// 按字节数取出字符串的长度 258
/// </summary> 259
/// <param name="strTmp">要计算的字符串</param> 260
/// <returns>字符串的字节数</returns> 261
public static int GetByteCount(string strTmp) 262
{ 263
int intCharCount = 0; 264
for (int i = 0; i < strTmp.Length; i++) 265
{ 266
if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3) 267
{ 268
intCharCount = intCharCount + 2; 269
} 270
else 271
{ 272
intCharCount = intCharCount + 1; 273
} 274
} 275
return intCharCount; 276
} 277
278
/// <summary> 279
/// 按字节数要在字符串的位置 280
/// </summary> 281
/// <param name="intIns">字符串的位置</param> 282
/// <param name="strTmp">要计算的字符串</param> 283
/// <returns>字节的位置</returns> 284
public static int GetByteIndex(int intIns, string strTmp) 285
{ 286
int intReIns = 0; 287
if (strTmp.Trim() == "") 288
{ 289
return intIns; 290
} 291
for (int i = 0; i < strTmp.Length; i++) 292
{ 293
if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3) 294
{ 295
intReIns = intReIns + 2; 296
} 297
else 298
{ 299
intReIns = intReIns + 1; 300
} 301
if (intReIns >= intIns) 302
{ 303
intReIns = i + 1; 304
break; 305
} 306
} 307
return intReIns; 308
} 309
310
/// <summary> 311
/// Method to make sure that user's inputs are not malicious 312
/// 截取输入最大的字符串 313
/// </summary> 314
/// <param name="text">User's Input</param> 315
/// <param name="maxLength">Maximum length of input</param> 316
/// <returns>The cleaned up version of the input</returns> 317
public static string InputText(string text, int maxLength) 318
{ 319
text = text.Trim(); 320
if (string.IsNullOrEmpty(text)) 321
return string.Empty; 322
if (text.Length > maxLength) 323
text = text.Substring(0, maxLength); 324
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces 325
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br> 326
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // 327
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags 328
text = text.Replace("'", "''"); 329
return text; 330
} 331
332
/// <summary> 333
/// Method to check whether input has other characters than numbers 334
/// 清楚字符 335
/// </summary> 336
public static string CleanNonWord(string text) 337
{ 338
return Regex.Replace(text, "\\W", ""); 339
} 340
341
//特殊字符的转换 342
public static string Encode(string str) 343
{ 344
345
str = str.Replace("'", "'"); 346
str = str.Replace("\"", """); 347
str = str.Replace("<", "<"); 348
str = str.Replace(">", ">"); 349
return str; 350
} 351
public static string Decode(string str) 352
{ 353
354
str = str.Replace(">", ">"); 355
str = str.Replace("<", "<"); 356
str = str.Replace(" ", " "); 357
str = str.Replace(""", "\""); 358
return str; 359
} 360
//字符传的转换 用在查询 登陆时 防止恶意的盗取密码 361
public static string TBCode(string strtb) 362
{ 363
strtb = strtb.Replace("!", ""); 364
strtb = strtb.Replace("@", ""); 365
strtb = strtb.Replace("#", ""); 366
strtb = strtb.Replace("$", ""); 367
strtb = strtb.Replace("%", ""); 368
strtb = strtb.Replace("^", ""); 369
strtb = strtb.Replace("&", ""); 370
strtb = strtb.Replace("*", ""); 371
strtb = strtb.Replace("(", ""); 372
strtb = strtb.Replace(")", ""); 373
strtb = strtb.Replace("_", ""); 374
strtb = strtb.Replace("+", ""); 375
strtb = strtb.Replace("|", ""); 376
strtb = strtb.Replace("?", ""); 377
strtb = strtb.Replace("/", ""); 378
strtb = strtb.Replace(".", ""); 379
strtb = strtb.Replace(">", ""); 380
strtb = strtb.Replace("<", ""); 381
strtb = strtb.Replace("{", ""); 382
strtb = strtb.Replace("}", ""); 383
strtb = strtb.Replace("[", ""); 384
strtb = strtb.Replace("]", ""); 385
strtb = strtb.Replace("-", ""); 386
strtb = strtb.Replace("=", ""); 387
strtb = strtb.Replace(",", ""); 388
return strtb; 389
} 390
391
//以javascript的windous.alert()方法输出提示信息 392
//strmsg 表示要输出的信息 393
//back 表示输出后是否要history.back() 394
//end 表示输出后是否要Response.End() 395
public static void WriteMessage(string strMsg, bool Back, bool End) 396
{ 397
string js = ""; 398
// Response = HttpContext.Current.Response; 399
//将单引号替换,防止js出错 400
strMsg = strMsg.Replace("'", ""); 401
//将回车符号换掉,防止js出错 402
strMsg = strMsg.Replace("\r\n", ""); 403
js="<script language=javascript>alert('" + strMsg + "')</script>"; 404
if (Back) 405
{ 406
js="<script language=javascript>history.back();</script)"; 407
} 408
if (End) 409
{ 410
js = "<script language=javascript>End</script)"; 411
} 412
HttpContext.Current.Response.Write(js); 413
} 414
415
/// <summary> 416
/// 小函数,将字符串转换成float型,若字符串为空,返回0 417
/// </summary> 418
/// <param name="str">要转换的字符串</param> 419
/// <returns>转后的float值</returns> 420
public static float GetFloatValue(string str) 421
{ 422
try 423
{ 424
if (str.Length == 0) 425
return 0; 426
else 427
return float.Parse(str); 428
} 429
catch 430
{ 431
WriteMessage("数据转换失败,请检查数据是否合理!", true, true); 432
return 0; 433
} 434
} 435
/// <summary> 436
/// 小函数,将字符串转换成int型,若字符串为空,返回0 437
/// </summary> 438
/// <param name="str">要转换的字符串</param> 439
/// <returns>转后的int值</returns> 440
public static int GetIntValue(string str) 441
{ 442
try 443
{ 444
if (str.Length == 0) 445
return 0; 446
else 447
{ 448
if (Int32.Parse(str) == 0) 449
return 0; 450
else 451
return Int32.Parse(str); 452
} 453
} 454
catch 455
{ 456
WriteMessage("数据转换失败,请检查数据是否合理!", true, true); 457
return 0; 458
} 459
} 460
} 461









