1 public sealed class StringTool 2 { 3 /// <summary> 4 /// 将txt文件读入字符串 5 /// </summary> 6 /// <param name="aPath"></param> 7 /// <returns></returns> 8 public static string ReadTextFile(string aPath) 9 { 10 string text = ""; 11 using (TextReader tr = new StreamReader(aPath)) 12 { 13 text = tr.ReadToEnd(); 14 } 15 return text; 16 } 17 18 /// <summary> 19 /// 将16进行编码的字符串写入到指定的文件路径 20 /// </summary> 21 /// <param name="aPath"></param> 22 /// <param name="aText"></param> 23 public static void WriteTextFile(string aPath, string aText) 24 { 25 using (TextWriter tw = new StreamWriter(aPath)) 26 { 27 tw.Write(aText); 28 } 29 } 30 31 /// <summary> 32 /// 序列化DataTable, 33 /// </summary> 34 /// <param name="aTable">需要序列化的DataTable</param> 35 /// <returns>成功返回非空字符串</returns> 36 public static string SerializerToXml(DataTable aTable) 37 { 38 StringBuilder sb = new StringBuilder(); 39 if (aTable == null) 40 return ""; 41 42 System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(System.Data.DataTable)); 43 System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb); 44 serializer.Serialize(writer, aTable); 45 46 return sb.ToString(); 47 } 48 49 /// <summary> 50 /// 反序列化DataTable表 51 /// </summary> 52 /// <param name="aXmlData">需要反序列化的DataTable的数据</param> 53 /// <returns>成功返回DataTable,失败返回null</returns> 54 public static DataTable DeSerializerFromXml(string aXmlData) 55 { 56 if (string.IsNullOrEmpty(aXmlData)) 57 return null; 58 59 System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(System.Data.DataTable)); 60 return (DataTable)serializer.Deserialize(new StringReader(aXmlData)); 61 } 62 63 /* 64 // 字符串进行16进制编码[decodeSTR],因为编码后位数不等,为了节约空间不进行等长操作,所以并以","分隔 65 // 与 StringTool.cs的函数相对应 66 function EncodeSTR(str) { 67 var t = ""; 68 for (var x = 0; x < str.length; x++) { 69 a = str.charCodeAt(x); 70 if (x != 0) { 71 t += ‘,‘; 72 } 73 t += a.toString(16).toUpperCase(); 74 } 75 return t; 76 } 77 //字符串[以","分隔]进行16进制解码,编码用[encodeSTR] 78 // 与 StringTool.cs的函数相对应 79 function DecodeSTR(str) { 80 var a1 = str.split(‘,‘); 81 for (var x = 0; x < a1.length; x++) { 82 a1[x] = String.fromCharCode(parseInt(a1[x], 16).toString(10)); 83 } 84 return a1.join(‘‘); 85 } 86 */ 87 /// <summary> 88 /// 字符串转为用,分隔的16进制字符串 解码用[decodeSTR] 与 StringTool.js的函数相对应 89 /// </summary> 90 /// <param name="str">字符串</param> 91 /// <param name="uppercase">大小写</param> 92 /// <returns>失败返回空字符串</returns> 93 static public string EncodeSTR(string str, bool uppercase = true) 94 { 95 if (string.IsNullOrEmpty(str)) 96 return ""; 97 Char[] aa = str.ToCharArray(); 98 StringBuilder sb = new StringBuilder(); 99 string strFormat = "{0:" + (uppercase == true ? "X" : "x") + "}"; 100 for (int i = 0; i < str.Length; i++) 101 { 102 sb.Append(String.Format(strFormat, Char.ConvertToUtf32(str, i))); 103 sb.Append(","); 104 } 105 if (sb.Length > 0) 106 sb.Remove(sb.Length - 1, 1); 107 return sb.ToString(); 108 } 109 110 /// <summary> 111 /// 用,分隔的16进制字符串转为字符串 编码用[EncodeSTR] 与 StringTool.js的函数相对应 112 /// </summary> 113 /// <param name="str">用,分隔的16进制字符串</param> 114 /// <returns>失败返回空字符串</returns> 115 static public string DecodeSTR(string str) 116 { 117 if (string.IsNullOrEmpty(str)) 118 return ""; 119 string[] aHexString = str.Split(new char[] { ‘,‘ }); 120 StringBuilder sb = new StringBuilder(); 121 for (int i = 0; i < aHexString.Length; i++) 122 { 123 sb.Append(Char.ConvertFromUtf32(Convert.ToInt32(aHexString[i], 16))); 124 } 125 return sb.ToString(); 126 } 127 128 /// <summary> 129 /// 将字符串转为16进制字符串,并进行了异或加密 130 /// </summary> 131 /// <param name="aStr"></param> 132 /// <returns></returns> 133 public static string StringToHexString(string aStr) 134 { 135 if (string.IsNullOrEmpty(aStr)) 136 return ""; 137 byte[] b = Encoding.Unicode.GetBytes(EncryptXOR(aStr)); 138 return ArrayToHexString(b); 139 } 140 141 /// <summary> 142 /// 将16进制字符串转为字符串,并进行了异或解密 143 /// </summary> 144 /// <param name="aStr"></param> 145 /// <returns></returns> 146 public static string HexStringToString(string aStr) 147 { 148 if (string.IsNullOrEmpty(aStr)) 149 return ""; 150 byte[] b = HexStringToArray(aStr); 151 return DecryptXOR(Encoding.Unicode.GetString(b)); 152 } 153 154 /// <summary> 155 /// 字节数组到16进制字符串 156 /// </summary> 157 /// <param name="array"></param> 158 /// <param name="uppercase">大小写</param> 159 /// <returns>失败返回空字符串</returns> 160 /// 161 static public string ArrayToHexString(byte[] array, bool uppercase = true) 162 { 163 if (array == null || array.Length < 1) return ""; 164 string format = "x2"; 165 if (uppercase) format = "X2"; 166 StringBuilder sb = new StringBuilder(); 167 foreach (byte b in array) sb.Append(b.ToString(format)); 168 return sb.ToString(); 169 } 170 171 /// <summary> 172 /// 将16进制字符串转为字节数组 173 /// </summary> 174 /// <param name="aHexString"></param> 175 /// <param name="uppercase"></param> 176 /// <returns></returns> 177 static public byte[] HexStringToArray(string aHexString, bool uppercase = true) 178 { 179 if (string.IsNullOrEmpty(aHexString)) 180 return null; 181 int count = aHexString.Length / 2; 182 byte[] b = new byte[count]; 183 for (int i = 0; i < count; i++) 184 { 185 string s = aHexString.Substring(i * 2, 2); 186 b[i] = byte.Parse(s, System.Globalization.NumberStyles.HexNumber); 187 } 188 189 return b; 190 } 191 192 /// <summary> 193 /// 在unicode 字符串中,中文的范围是在4E00..9FFF 194 // 通过对字符的unicode编码进行判断来确定字符是否为中文。 195 /// </summary> 196 /// <param name="word"></param> 197 /// <returns></returns> 198 static public bool IsChineseLetter(string word) 199 { 200 int code = 0; 201 int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend) 202 int chend = Convert.ToInt32("9fff", 16); 203 if (word != "") 204 { 205 code = Char.ConvertToUtf32(word, 0); //获得字符word中指定索引index处字符unicode编码 206 if (code >= chfrom && code <= chend) 207 return true; //当code在中文范围内返回true 208 else 209 return false; //当code不在中文范围内返回false 210 } 211 return false; 212 } 213 214 /// <summary> 215 /// 判断句子中是否含有中文 216 /// </summary> 217 /// <param >字符串</param> 218 /// <returns></returns> 219 static public bool WordsInChinese(string aStr) 220 { 221 Regex rx = new Regex("^[\u4e00-\u9fa5]$"); 222 for (int i = 0; i < aStr.Length; i++) 223 { 224 if (rx.IsMatch(aStr[i].ToString())) 225 return true; 226 } 227 return false; 228 } 229 230 /// <summary> 231 /// 给定一个字符串,判断其是否只包含有汉字 232 /// </summary> 233 /// <param name="aStr"></param> 234 /// <returns></returns> 235 static public bool IsOnlyContainsChinese(string aStr) 236 { 237 char[] words = aStr.ToCharArray(); 238 foreach (char word in words) 239 { 240 if (IsGBCode(word.ToString()) || IsGBKCode(word.ToString())) // it is a GB2312 or GBK chinese word 241 continue; 242 else 243 return false; 244 } 245 return true; 246 } 247 248 /// <summary> 249 /// 判断一个word是否为GB2312编码的汉字 250 /// </summary> 251 /// <param name="word"></param> 252 /// <returns></returns> 253 static public bool IsGBCode(string word) 254 { 255 byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word); 256 if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code 257 return false; 258 else 259 { 260 byte byte1 = bytes[0]; 261 byte byte2 = bytes[1]; 262 if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //判断是否是GB2312 263 return true; 264 else 265 return false; 266 } 267 } 268 269 /// <summary> 270 /// 判断一个word是否为GBK编码的汉字 271 /// </summary> 272 /// <param name="word"></param> 273 /// <returns></returns> 274 static public bool IsGBKCode(string word) 275 { 276 byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(word.ToString()); 277 if (bytes.Length <= 1) // if there is only one byte, it is ASCII code 278 return false; 279 else 280 { 281 byte byte1 = bytes[0]; 282 byte byte2 = bytes[1]; 283 if (byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254) //判断是否是GBK编码 284 return true; 285 else 286 return false; 287 } 288 } 289 290 /// <summary> 291 /// 判断一个word是否为Big5编码的汉字 292 /// </summary> 293 /// <param name="word"></param> 294 /// <returns></returns> 295 static public bool IsBig5Code(string word) 296 { 297 byte[] bytes = Encoding.GetEncoding("Big5").GetBytes(word.ToString()); 298 if (bytes.Length <= 1) // if there is only one byte, it is ASCII code 299 return false; 300 else 301 { 302 byte byte1 = bytes[0]; 303 byte byte2 = bytes[1]; 304 if ((byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 254))) //判断是否是Big5编码 305 return true; 306 else 307 return false; 308 } 309 } 310 311 /// <summary> 312 /// 对字符串进行简单的异或加密 313 /// </summary> 314 /// <param name="aStr"></param> 315 /// <param name="aXor"></param> 316 /// <returns>失败返回空字符串</returns> 317 static public string EncryptXOR(string aStr, byte aXor = 0x11) 318 { 319 if (String.IsNullOrEmpty(aStr)) 320 return ""; 321 byte[] bb = Encoding.Unicode.GetBytes(aStr); 322 for (int i = 0; i < bb.Length; i++) 323 bb[i] ^= aXor; 324 return Encoding.Unicode.GetString(bb); 325 } 326 327 /// <summary> 328 /// 对字符串进行简单的异或解密 329 /// </summary> 330 /// <param name="aStr"></param> 331 /// <param name="aXor"></param> 332 /// <returns>失败返回空字符串</returns> 333 static public string DecryptXOR(string aStr, byte aXor = 0x11) 334 { 335 if (String.IsNullOrEmpty(aStr)) 336 return ""; 337 byte[] bb = Encoding.Unicode.GetBytes(aStr); 338 for (int i = 0; i < bb.Length; i++) 339 bb[i] ^= aXor; 340 return Encoding.Unicode.GetString(bb); 341 } 342 343 /// <summary> 344 /// 获得字符串中开始和结束字符串中间得字符 345 /// </summary> 346 /// <param name="str">字符串</param> 347 /// <param name="s">开始</param> 348 /// <param name="e">结束</param> 349 /// </summary> 350 public static string GetSubString(string str, string s, string e) 351 { 352 Regex rg = new Regex("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline); 353 return rg.Match(str).Value; 354 } 355 356 /// <summary> 357 /// 从最后一位验证前面17位的18位身份证号码 358 /// </summary> 359 /// <param name="id"></param> 360 /// <returns></returns> 361 public static bool CheckCardId(string id) 362 { 363 /* 身份证号码的验证及15位升18位算法 364 18位身份证标准在国家质量技术监督局于1999年7月1日实施的GB11643-1999《公民身份号码》中做了明确的规定。 GB11643-1999《公民身份号码》为GB11643-1989《社会保障号码》的修订版,其中指出将原标准名称"社会保障号码"更名为"公民身份号码",另外GB11643-1999《公民身份号码》从实施之日起代替GB11643-1989。GB11643-1999《公民身份号码》主要内容如下: 365 一、范围 366 该标准规定了公民身份号码的编码对象、号码的结构和表现形式,使每个编码对象获得一个唯一的、不变的法定号码。 367 二、编码对象 368 公民身份号码的编码对象是具有*国籍的公民。 369 三、号码的结构和表示形式 370 1、号码的结构 371 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。 372 2、地址码 373 表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。 374 3、出生日期码 375 表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。 376 4、顺序码 377 表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。 378 5、校验码 379 (1)十七位数字本体码加权求和公式 380 S = Sum(Ai * Wi),先对前17位数字的权求和 381 Ai:表示第i位置上的身份证号码数字值 382 Wi:表示第i位置上的加权因子 383 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 384 (2)计算模 Y = mod(S, 11) 385 (3)通过模得到对应的校验码 386 Y: 0 1 2 3 4 5 6 7 8 9 10 387 校验码: 1 0 X 9 8 7 6 5 4 3 2 388 四、举例如下: 389 北京市朝阳区: 11010519491231002X 390 广东省汕头市: 440524188001010014 391 */ 392 393 int[] wQuan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; 394 string checkWei = "10X98765432"; 395 396 string number17 = id.Substring(0, 17); 397 string number18 = id.Substring(17); 398 399 int sum = 0; 400 for (int i = 0; i < 17; i++) 401 sum = sum + Convert.ToInt32(number17[i].ToString()) * wQuan[i]; 402 403 int mod = sum % 11; 404 string result = checkWei[mod].ToString(); 405 if (number18.Equals(result, StringComparison.OrdinalIgnoreCase)) 406 { 407 return true; 408 } 409 else 410 { 411 return false; 412 } 413 } 414 415 /// <summary> 416 /// 生成一个序列号,根据Guid生成,最短1位,最长33位,默认是4位 417 /// </summary> 418 /// <param name="aLength"></param> 419 /// <returns></returns> 420 public static string CreateSerialNumber(int aLength = 4) 421 { 422 if (aLength < 1) 423 aLength = 1; 424 if (aLength > 33) 425 aLength = 33; 426 427 string str = string.Empty; 428 string codeStr = string.Empty; 429 string guidStr = Guid.NewGuid().ToString().Replace("-", ""); 430 int guidStrLen = guidStr.Length; 431 Random rnd = new Random(int.Parse(DateTime.Now.ToString("MMddHHmmsss"))); 432 433 for (int i = 0; i < aLength; i++) 434 { 435 int index = rnd.Next(0, guidStrLen - 1); 436 str += guidStr.Substring(index, 1); 437 } 438 439 return str.ToUpper(); 440 } 441 442 /// <summary> 443 /// 反转字符串 444 /// </summary> 445 /// <param name="aString"></param> 446 /// <returns></returns> 447 public static string ReverseString(string aString) 448 { 449 if (string.IsNullOrEmpty(aString)) 450 return ""; 451 Char[] LS_Str = aString.ToCharArray(); 452 Array.Reverse(LS_Str); 453 454 return new String(LS_Str);//反转字符串 455 } 456 457 /// <summary> 458 /// 全角转半角 459 /// </summary> 460 /// <param name="aString"></param> 461 /// <returns></returns> 462 public static string QjToBj(string aString) 463 { 464 string QJstr = aString; 465 char[] c = QJstr.ToCharArray(); 466 for (int i = 0; i < c.Length; i++) 467 { 468 byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1); 469 if (b.Length == 2) 470 { 471 if (b[1] == 255) 472 { 473 b[0] = (byte)(b[0] + 32); 474 b[1] = 0; 475 c[i] = System.Text.Encoding.Unicode.GetChars(b)[0]; 476 } 477 } 478 } 479 string strNew = new string(c); 480 return strNew; 481 } 482 483 /// <summary> 484 /// 获得汉字拼音的的第一个字母 485 /// </summary> 486 /// <param name="aText"></param> 487 /// <returns></returns> 488 static public string GetChineseSpellFirstLetter(string aText) 489 { 490 int len = aText.Length; 491 string myStr = ""; 492 for (int i = 0; i < len; i++) 493 { 494 myStr += GetSpell(aText[i]); 495 } 496 return myStr.ToLower(); 497 } 498 499 /// <summary> 500 /// 利用汉字在计算机里面的编码来的到汉字的拼音。 501 /// </summary> 502 /// <param name="aChar"></param> 503 /// <returns></returns> 504 static public string GetSpell(char aChar) 505 { 506 byte[] arrCN = Encoding.Default.GetBytes(aChar.ToString()); 507 if (arrCN.Length > 1) 508 { 509 int area = (short)arrCN[0]; 510 int pos = (short)arrCN[1]; 511 int code = (area << 8) + pos; 512 int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 }; 513 for (int i = 0; i < 26; i++) 514 { 515 int max = 55290; 516 if (i != 25) max = areacode[i + 1]; 517 if (areacode[i] <= code && code < max) 518 { 519 return Encoding.Default.GetString(new byte[] { (byte)(65 + i) }); 520 } 521 } 522 return aChar.ToString(); 523 } 524 else 525 return aChar.ToString(); 526 } 527 }