stringutil.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Ant.Frame
  6. {
  7. /// <summary>
  8. /// StringUtil 的摘要说明。
  9. /// </summary>
  10. public class StringUtil
  11. {
  12. public StringUtil()
  13. {
  14. //
  15. // TODO: 在此处添加构造函数逻辑
  16. //
  17. }
  18. /// <summary>
  19. /// 功能描述:将字符串转换成参数。
  20. /// </summary>
  21. /// <param name="str">需转换的字符串</param>
  22. /// <returns></returns>
  23. public static string paramChg(string str)
  24. {
  25. str = "@" + str;
  26. return str;
  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) == -1)
  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 sourceStringSub = sourceString.Substring(startIndex);
  45. if (sourceStringSub.ToUpper() == removedString.ToUpper())
  46. {
  47. result = sourceString.Remove(startIndex, LengthOfremovedString);
  48. }
  49. return result;
  50. }
  51. catch
  52. {
  53. return sourceString;
  54. }
  55. }
  56. /// <summary>
  57. /// 功能描述:从字符串中的指定位置删除指定的字符串。
  58. /// </summary>
  59. /// <param name="sourceString">原字符串</param>
  60. /// <param name="removedString">移除字符串</param>
  61. /// <returns>留下的字符串</returns>
  62. public static string StrRemove(string sourceString, string removedString)
  63. {
  64. try
  65. {
  66. if (sourceString.IndexOf(removedString) == -1)
  67. throw new Exception("原字符串中不包含移除字符串!");
  68. string result = sourceString;
  69. int LengthOfremovedString = removedString.Length;
  70. if (sourceString.IndexOf(removedString) > 0)
  71. {
  72. int startIndex = sourceString.IndexOf(removedString);
  73. result = sourceString.Remove(startIndex, LengthOfremovedString);
  74. }
  75. return result;
  76. }
  77. catch
  78. {
  79. return sourceString;
  80. }
  81. }
  82. /// <summary>
  83. /// 功能描述:获取拆分符右边的字符串。
  84. /// </summary>
  85. /// <param name="sourceString">原字符串</param>
  86. /// <param name="splitChar">拆分字符</param>
  87. /// <returns>右边的字符串</returns>
  88. public static string RightSplit(string sourceString, char splitChar)
  89. {
  90. string result = null;
  91. string[] tempStr = sourceString.Split(splitChar);
  92. if (tempStr.Length > 0)
  93. {
  94. result = tempStr[tempStr.Length - 1].ToString();
  95. }
  96. return result;
  97. }
  98. /// <summary>
  99. /// 功能描述:获取拆分符左边的字符串。
  100. /// </summary>
  101. /// <param name="sourceString">原字符串</param>
  102. /// <param name="splitChar">拆分字符</param>
  103. /// <returns>左边的字符串</returns>
  104. public static string LeftSplit(string sourceString, char splitChar)
  105. {
  106. string result = null;
  107. string[] tempStr = sourceString.Split(splitChar);
  108. if (tempStr.Length > 0)
  109. {
  110. result = tempStr[0].ToString();
  111. }
  112. return result;
  113. }
  114. /// <summary>
  115. /// 功能描述:去掉最后一个逗号后面的字符串。
  116. /// </summary>
  117. /// <param name="sourceString">原字符串</param>
  118. /// <param name="splitChar">拆分字符</param>
  119. /// <returns>左边的字符串</returns>
  120. public static string DelLsatComma(string sourceString)
  121. {
  122. if(sourceString.EndsWith(","))
  123. {
  124. sourceString = sourceString.TrimEnd(',');
  125. }
  126. return sourceString;
  127. //if (sourceString.IndexOf(",") == -1)
  128. //{
  129. // return sourceString;
  130. //}
  131. //return sourceString.Substring(0, sourceString.LastIndexOf(","));
  132. }
  133. /// <summary>
  134. /// 功能描述:删除不可见字符。
  135. /// </summary>
  136. /// <param name="sourceString">原字符串</param>
  137. /// <returns></returns>
  138. public static string DeleteUnVisibleChar(string sourceString)
  139. {
  140. System.Text.StringBuilder strBuilder = new System.Text.StringBuilder(131);
  141. for (int i = 0; i < sourceString.Length; i++)
  142. {
  143. int Unicode = sourceString[i];
  144. if (Unicode >= 16)
  145. {
  146. strBuilder.Append(sourceString[i].ToString());
  147. }
  148. }
  149. return strBuilder.ToString();
  150. }
  151. /// <summary>
  152. /// 功能描述:获取数组元素的合并字符串。
  153. /// </summary>
  154. /// <param name="stringArray">字符串数组</param>
  155. /// <returns></returns>
  156. public static string GetArrayString(string[] stringArray)
  157. {
  158. string totalString = null;
  159. for (int i = 0; i < stringArray.Length; i++)
  160. {
  161. totalString = totalString + stringArray[i];
  162. }
  163. return totalString;
  164. }
  165. /// <summary>
  166. /// 功能描述:获取某一字符串在字符串数组中出现的次数。
  167. /// </summary>
  168. /// <param name="stringArray">字符串数组</param>
  169. /// <param name="findString">某一字符串</param>
  170. /// <returns></returns>
  171. public static int GetStringCount(string[] stringArray, string findString)
  172. {
  173. int count = 0;
  174. string totalString = GetArrayString(stringArray);
  175. int findStringLength = findString.Length;
  176. string subString = totalString;
  177. while (subString.IndexOf(findString) >= 0)
  178. {
  179. subString = subString.Substring(subString.IndexOf(findString) + findStringLength);
  180. count += 1;
  181. }
  182. return count;
  183. }
  184. /// <summary>
  185. /// 功能描述:获取某一字符串在字符串中出现的次数。
  186. /// </summary>
  187. /// <param name="sourceString">字符串</param>
  188. /// <param name="findString">某一字符串</param>
  189. /// <returns></returns>
  190. public static int GetStringCount(string sourceString, string findString)
  191. {
  192. int count = 0;
  193. int findStringLength = findString.Length;
  194. string subString = sourceString;
  195. while (subString.IndexOf(findString) >= 0)
  196. {
  197. subString = subString.Substring(subString.IndexOf(findString) + findStringLength);
  198. count += 1;
  199. }
  200. return count;
  201. }
  202. /// <summary>
  203. /// 功能描述:截取从startString开始到原字符串结尾的所有字符。
  204. /// </summary>
  205. /// <param name="sourceString">字符串</param>
  206. /// <param name="startString">某一字符串</param>
  207. /// <returns></returns>
  208. public static string GetSubstring(string sourceString, string startString)
  209. {
  210. int startIndex = sourceString.IndexOf(startString);
  211. if (startIndex > 0)
  212. return sourceString.Substring(startIndex);
  213. return sourceString;
  214. }
  215. /// <summary>
  216. /// 功能描述:按字节数取出字符串的长度。
  217. /// </summary>
  218. /// <param name="sourceString">要计算的字符串</param>
  219. /// <returns>字符串的字节数</returns>
  220. public static int GetByteCount(string sourceString)
  221. {
  222. int itnCharCount = 0;
  223. for (int i = 0; i < sourceString.Length; i++)
  224. {
  225. if (System.Text.UTF8Encoding.UTF8.GetByteCount(sourceString.Substring(i, 1)) == 3)
  226. {
  227. itnCharCount = itnCharCount + 2;
  228. }
  229. else
  230. {
  231. itnCharCount = itnCharCount + 1;
  232. }
  233. }
  234. return itnCharCount;
  235. }
  236. /// <summary>
  237. /// 功能描述:按字节数要在字符串的位置。
  238. /// </summary>
  239. /// <param name="intIns">字符串的位置</param>
  240. /// <param name="strTmp">要计算的字符串</param>
  241. /// <returns>字节的位置</returns>
  242. public static int GetByteIndex(int intIns, string strTmp)
  243. {
  244. int intReIns = 0;
  245. if (strTmp.Trim() == "")
  246. {
  247. return intIns;
  248. }
  249. for (int i = 0; i < strTmp.Length; i++)
  250. {
  251. if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3)
  252. {
  253. intReIns = intReIns + 2;
  254. }
  255. else
  256. {
  257. intReIns = intReIns + 1;
  258. }
  259. if (intReIns >= intIns)
  260. {
  261. intReIns = i + 1;
  262. break;
  263. }
  264. }
  265. return intReIns;
  266. }
  267. /// <summary>
  268. /// 返回路径分割符号"/"最后一个字符串。
  269. /// </summary>
  270. /// <param name="sourceStr">原字符串</param>
  271. /// <param name="splitChar">分割符号</param>
  272. /// <returns></returns>
  273. public static string getLastStr(string sourceStr, char splitChar)
  274. {
  275. string[] strArr = sourceStr.Split(splitChar);
  276. string lastStr = strArr[strArr.Length - 1];
  277. if (lastStr == "")
  278. return strArr[strArr.Length - 2];
  279. return lastStr;
  280. }
  281. /// <summary>
  282. /// 获取路径最后一个分隔符"/"左边的全部字符串
  283. /// </summary>
  284. /// <param name="sourceStr"></param>
  285. /// <param name="splitChar">分隔符</param>
  286. /// <returns></returns>
  287. public static string getLeftStr(string sourceStr, char splitChar)
  288. {
  289. string[] strArr = sourceStr.Split(splitChar);
  290. int length = sourceStr.Length - getLastStr(sourceStr, '/').Length;
  291. string leftStr = sourceStr.Substring(0, length);
  292. return leftStr;
  293. }
  294. /// <summary>
  295. /// 编码字符串
  296. /// </summary>
  297. /// <param name="str"></param>
  298. /// <returns></returns>
  299. public static string Encode(string str)
  300. {
  301. str = str.Replace("<p>", "<br>");
  302. str = str.Replace("</p>", "");
  303. str = str.Replace(".", ".");
  304. str = str.Replace("'", "''");
  305. str = str.Replace(":", ":");
  306. // str = str.Replace("<", "<");
  307. // str = str.Replace(">", ">");
  308. str = str.Replace("//", "//");
  309. str = str.Replace("http", "http");
  310. str = str.Replace("js", "js");
  311. str = str.Replace("gif", "gif");
  312. return str;
  313. }
  314. public static string SqlEncode(string str)
  315. {
  316. str = str.Replace("'", "''");
  317. return str;
  318. }
  319. /// <summary>
  320. /// 解码字符串
  321. /// </summary>
  322. /// <param name="str"></param>
  323. /// <returns></returns>
  324. public static string Decode(string str)
  325. {
  326. str = str.Replace("<br>", "/n");
  327. //str = str.Replace(">", ">");
  328. //str = str.Replace("<", "<");
  329. str = str.Replace(" ", " ");
  330. str = str.Replace('"', '/');
  331. return str;
  332. }
  333. /// <summary>
  334. /// 文本域的html编码
  335. /// </summary>
  336. /// <param name="str"></param>
  337. /// <returns></returns>
  338. public static string HtmlEncode(string str)
  339. {
  340. str = str.Replace("'", "''");
  341. str = str.Replace('"', '/');
  342. str = str.Replace(" ", " ");
  343. // str = str.Replace("<", "<");
  344. // str = str.Replace(">", ">");
  345. str = str.Replace("/n", "<br>");
  346. str = str.Replace("//", "//");
  347. str = str.Replace("http", "http");
  348. str = str.Replace("js", "js");
  349. str = str.Replace("gif", "gif");
  350. str = str.Replace("com", "com");
  351. str = str.Replace(".", ".");
  352. return str;
  353. }
  354. /// <summary>
  355. /// 文本域的html解码
  356. /// </summary>
  357. /// <param name="str"></param>
  358. /// <returns></returns>
  359. public static string HtmlDecode(string str)
  360. {
  361. // str = str.Replace(">", ">");
  362. // str = str.Replace("<", "<");
  363. str = str.Replace(" ", " ");
  364. str = str.Replace('"', '/');
  365. str = str.Replace("''", "'");
  366. str = str.Replace("<br>", "/n");
  367. return str;
  368. }
  369. /// <summary>
  370. /// 出去空格,并见对尖括号内的空格加入。
  371. /// </summary>
  372. /// <param name="contStr"></param>
  373. /// <returns></returns>
  374. public static string ReplaceStr(string contStr)
  375. {
  376. contStr = System.Text.RegularExpressions.Regex.Replace(contStr, "//s", " ");
  377. int y, z;
  378. z = 0;
  379. int i = 0;
  380. do
  381. {
  382. y = contStr.IndexOf("<", z);
  383. if (y >= 0)
  384. {
  385. z = contStr.IndexOf(">", y);
  386. if (z >= 0)
  387. {
  388. i += contStr.Substring(y, z - y + 1).Replace(" ", " ").Length + 4; //统计超级链接标签占用的字符数
  389. string tStr1, tStr2;
  390. tStr1 = contStr.Substring(0, z + 1);
  391. tStr2 = contStr.Substring(z + 1);
  392. contStr = tStr1.Replace(tStr1.Substring(y, z - y + 1), tStr1.Substring(y, z - y + 1).Replace(" ", " ")) + tStr2;
  393. }
  394. else
  395. {
  396. z = y + 1;
  397. if (z > contStr.Length - 1)
  398. break;
  399. }
  400. }
  401. }
  402. while (y >= 0 && z <= contStr.Length - 1);
  403. return contStr;
  404. }
  405. }
  406. }