UtilStr.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Drawing.Imaging;
  5. using System.Collections;
  6. using System.Drawing.Printing;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. namespace SysBaseLibs
  10. {
  11. public class UtilStr
  12. {
  13. /// <summary>
  14. /// 得到该对象的字符串,在字符串的两边加上“[”“]”
  15. /// </summary>
  16. /// <param name="poObject">处理的对象</param>
  17. /// <returns>字符串</returns>
  18. public static string GetDelimitedStr(object poObject)
  19. {
  20. return ("[" + poObject.ToString() + "]");
  21. }
  22. /// <summary>
  23. /// 连接连个对象的字符串型,并把他们连接成一个字符串返回出来。
  24. /// </summary>
  25. /// <param name="poFirst"></param>
  26. /// <param name="poSecond"></param>
  27. /// <returns></returns>
  28. public static string JoinString(object poFirst, object poSecond)
  29. {
  30. return string.Format("[{0}],[{1}]", poFirst.ToString().Trim(), poSecond.ToString().Trim());
  31. }
  32. /// <summary>
  33. /// 将一个对象以字符串形式与另外一个字符串进行连接,并添加“[”,“]”标识符号
  34. /// </summary>
  35. /// <param name="pcSource">被连接的字符串</param>
  36. /// <param name="poTobeAppended">连接的字符串</param>
  37. /// <returns>返回值</returns>
  38. public static string AddStr(string pcSource, object poTobeAppended)
  39. {
  40. string lcStr = GetDelimitedStr(poTobeAppended);
  41. pcSource = pcSource.Trim();
  42. if (pcSource == "")
  43. {
  44. return lcStr;
  45. }
  46. if ((pcSource[0] == '[') && (pcSource[pcSource.Length - 1] == ']'))
  47. {
  48. return (pcSource + string.Format(",[{0}]", poTobeAppended.ToString().Trim()));
  49. }
  50. return JoinString(pcSource, poTobeAppended);
  51. }
  52. /// <summary>
  53. /// 直接连接两个字符串
  54. /// </summary>
  55. /// <param name="pcSource"></param>
  56. /// <param name="poTobeAppended"></param>
  57. /// <returns></returns>
  58. public static string AppendToStr(string pcSource, object poTobeAppended)
  59. {
  60. string lcStr = GetDelimitedStr(poTobeAppended);
  61. pcSource = pcSource.Trim();
  62. if (pcSource != "")
  63. {
  64. lcStr = pcSource + "," + lcStr;
  65. }
  66. return lcStr;
  67. }
  68. /// <summary>
  69. /// 将一个Array转换成字符串,默认分割符为","
  70. /// </summary>
  71. /// <param name="paStr"></param>
  72. /// <returns></returns>
  73. public static string ArrayToStr(Array paStr)
  74. {
  75. return ArrayToStrEx(paStr, ",");
  76. }
  77. /// <summary>
  78. /// 根据传入的分割符号将Array 转换成字符串
  79. /// </summary>
  80. /// <param name="paStr"></param>
  81. /// <param name="separator"></param>
  82. /// <returns></returns>
  83. public static string ArrayToStrEx(Array paStr, string pcSeparator)
  84. {
  85. string lcStr = "";
  86. foreach (string lcStr2 in paStr)
  87. {
  88. lcStr = lcStr + ((lcStr == "") ? "" : pcSeparator) + GetDelimitedStr(lcStr2);
  89. }
  90. return lcStr;
  91. }
  92. /// <summary>
  93. /// 将字符串转换成Array 对象 ,以","为分割符。
  94. /// </summary>
  95. /// <param name="str"></param>
  96. /// <returns></returns>
  97. public static Array StrToArray(string str)
  98. {
  99. return StrToArrayEx(str, ",");
  100. }
  101. /// <summary>
  102. /// 将一个字符串以分割符转换成Array对象,如 a,[b,[c,d]],e 将分割成 a ; b,[c,d] ; e 三个串
  103. /// </summary>
  104. /// <param name="str"></param>
  105. /// <param name="separator"></param>
  106. /// <returns></returns>
  107. public static Array StrToArrayEx(string str, string separator)
  108. {
  109. ArrayList list1 = new ArrayList();
  110. str = (str == null) ? "" : str.Trim();
  111. int num1 = separator.Length;
  112. while (str.Length > 0)
  113. {
  114. int num2 = str.IndexOf(separator);
  115. int num3 = str.IndexOf("[");
  116. if ((num3 >= 0) && (num3 < num2))
  117. {
  118. int num4 = 1;
  119. int num5 = str.Length;
  120. int num6 = num3 + 1;
  121. while (num6 < num5)
  122. {
  123. switch (str[num6])
  124. {
  125. case '[':
  126. num4++;
  127. goto Label_0082;
  128. case '\\':
  129. goto Label_0082;
  130. case ']':
  131. break;
  132. default:
  133. goto Label_0082;
  134. }
  135. num4--;
  136. Label_0082:
  137. if (num4 == 0)
  138. {
  139. break;
  140. }
  141. num6++;
  142. }
  143. num2 = str.IndexOf(separator, num6);
  144. }
  145. if (num2 < 0)
  146. {
  147. num2 = str.Length;
  148. }
  149. string text1 = str.Substring(0, num2);
  150. int num7 = text1.Length;
  151. if (((num7 > 0) && (text1[0] == '[')) && (text1[num7 - 1] == ']'))
  152. {
  153. text1 = text1.Substring(1, num7 - 2);
  154. }
  155. list1.Add(text1.Trim());
  156. if ((num2 + num1) > str.Length)
  157. {
  158. str = "";
  159. }
  160. else
  161. {
  162. str = str.Substring(num2 + num1).Trim();
  163. }
  164. }
  165. Array array1 = Array.CreateInstance(typeof(string), list1.Count);
  166. list1.CopyTo(array1);
  167. return array1;
  168. }
  169. /// <summary>
  170. /// 将字符串转换成 Color 对象
  171. /// </summary>
  172. /// <param name="str"></param>
  173. /// <returns></returns>
  174. public static Color colorFromStr(string str)
  175. {
  176. Color color1 = Color.Black;
  177. Array array1 = StrToArray(str);
  178. int num1 = 0;
  179. int num2 = 0;
  180. int num3 = 0;
  181. if (array1.Length >= 3)
  182. {
  183. num1 = (int)Utils.ValD((string)array1.GetValue(0));
  184. num2 = (int)Utils.ValD((string)array1.GetValue(1));
  185. num3 = (int)Utils.ValD((string)array1.GetValue(2));
  186. color1 = Color.FromArgb(num1, num2, num3);
  187. }
  188. return color1;
  189. }
  190. /// <summary>
  191. /// Translate Color to String
  192. /// </summary>
  193. /// <param name="color"></param>
  194. /// <returns></returns>
  195. public static string colorToStr(Color color)
  196. {
  197. return (color.R.ToString() + "," + color.G.ToString() + "," + color.B.ToString());
  198. }
  199. /// <summary>
  200. /// 从pcSource 末尾移除 pcRemoveStr
  201. /// </summary>
  202. /// <param name="pcSource"></param>
  203. /// <param name="pcRemoveStr"></param>
  204. /// <returns></returns>
  205. public static string TrimEnd(string pcSource, string pcRemoveStr)
  206. {
  207. char[] chArray1 = pcRemoveStr.ToCharArray();
  208. return pcSource.TrimEnd(chArray1);
  209. }
  210. /// <summary>
  211. /// 字符串转换成日期格式
  212. /// </summary>
  213. /// <param name="pcStr"></param>
  214. /// <returns></returns>
  215. public static DateTime StrToDt(string pcStr)
  216. {
  217. DateTime time1 = new DateTime(1900, 1, 1);
  218. try
  219. {
  220. return DateTime.Parse(pcStr);
  221. }
  222. catch (Exception)
  223. {
  224. }
  225. return new DateTime(1900, 1, 1);
  226. }
  227. /// <summary>
  228. /// 字符串转换成日期格式
  229. /// </summary>
  230. /// <param name="pcStr"></param>
  231. /// <returns></returns>
  232. public static DateTime StrToDt(string pcStr,string format)
  233. {
  234. DateTime time1 = new DateTime(1900, 1, 1);
  235. try
  236. {
  237. DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
  238. dtFormat.ShortDatePattern = format;
  239. //DateTime dt = DateTime.ParseExact(pcStr, format, System.Globalization.CultureInfo.CurrentCulture);
  240. DateTime dt = Convert.ToDateTime(pcStr, dtFormat);//DateTime dt
  241. return dt;
  242. }
  243. catch (Exception err)
  244. {
  245. ThreadLog.LogException(err);
  246. }
  247. return new DateTime(1900, 1, 1);
  248. }
  249. /// <summary>
  250. /// 将 piCount 个 pcString 串成一个字符串
  251. /// </summary>
  252. /// <param name="pcString"></param>
  253. /// <param name="piCount"></param>
  254. /// <returns></returns>
  255. public static string Replicate(string pcString, int piCount)
  256. {
  257. string text1 = "";
  258. for (int i = 0; i < piCount; i++)
  259. {
  260. text1 = text1 + pcString;
  261. }
  262. return text1;
  263. }
  264. /// <summary>
  265. /// Translate RectangleF to string
  266. /// </summary>
  267. /// <param name="rect"></param>
  268. /// <returns></returns>
  269. public static string rectToStr(RectangleF rect)
  270. {
  271. return (rect.X.ToString() + "," + rect.Y.ToString() + "," + rect.Width.ToString() + "," + rect.Height.ToString());
  272. }
  273. /// <summary>
  274. /// Translate string to RectangleF
  275. /// </summary>
  276. /// <param name="str"></param>
  277. /// <returns></returns>
  278. public static RectangleF rectFromStr(string str)
  279. {
  280. Array array1 = StrToArray(str);
  281. RectangleF ef1 = (RectangleF)new Rectangle(0, 0, 0, 0);
  282. if (array1.Length >= 4)
  283. {
  284. ef1.X = (float)Utils.ValD((string)array1.GetValue(0));
  285. ef1.Y = (float)Utils.ValD((string)array1.GetValue(1));
  286. ef1.Width = (float)Utils.ValD((string)array1.GetValue(2));
  287. ef1.Height = (float)Utils.ValD((string)array1.GetValue(3));
  288. }
  289. return ef1;
  290. }
  291. /// <summary>
  292. /// PrinterSettings To String
  293. /// </summary>
  294. /// <param name="poSetting"></param>
  295. /// <returns></returns>
  296. public static string PrinterSettingToStr(PrinterSettings poSetting)
  297. {
  298. string text1 = "";
  299. text1 = AppendToStr(text1, "PrinterName=" + poSetting.PrinterName);
  300. text1 = AppendToStr(text1, "FromPage=" + poSetting.FromPage.ToString());
  301. text1 = AppendToStr(text1, "ToPage=" + poSetting.ToPage.ToString());
  302. return AppendToStr(text1, "Copies=" + poSetting.Copies.ToString());
  303. }
  304. /// <summary>
  305. /// String To PrinterSettings
  306. /// </summary>
  307. /// <param name="pcString"></param>
  308. /// <returns></returns>
  309. public static PrinterSettings PrinterSettingFromStr(string pcString)
  310. {
  311. PrinterSettings settings1 = new PrinterSettings();
  312. Array array1 = StrToArray(pcString);
  313. foreach (string text1 in array1)
  314. {
  315. Array array2 = StrToArrayEx(text1, "=");
  316. if (array2.Length == 2)
  317. {
  318. string text4;
  319. string text2 = array2.GetValue(0).ToString();
  320. string text3 = array2.GetValue(1).ToString();
  321. if ((text4 = text2) != null)
  322. {
  323. text4 = string.IsInterned(text4);
  324. if (text4 == "PrinterName")
  325. {
  326. settings1.PrinterName = text3;
  327. }
  328. else
  329. {
  330. if (text4 == "FromPage")
  331. {
  332. settings1.FromPage = Utils.ValI(text3);
  333. continue;
  334. }
  335. if (text4 == "ToPage")
  336. {
  337. settings1.ToPage = Utils.ValI(text3);
  338. continue;
  339. }
  340. if (text4 == "Copies")
  341. {
  342. settings1.Copies = (short)Utils.ValI(text3);
  343. }
  344. }
  345. }
  346. }
  347. }
  348. return settings1;
  349. }
  350. public static string PrinterPageSettingToStr(PrinterSettings poPrinter, PageSettings poPage)
  351. {
  352. if (poPage == null)
  353. {
  354. return ("[" + PrinterSettingToStr(poPrinter) + "],[Default]");
  355. }
  356. return ("[" + PrinterSettingToStr(poPrinter) + "],[" + PageSettingToStr(poPage) + "]");
  357. }
  358. public static string PageSettingToStr(PageSettings poSetting)
  359. {
  360. string text1 = "";
  361. text1 = AppendToStr(text1, "LeftMargin=" + poSetting.Margins.Left.ToString());
  362. text1 = AppendToStr(text1, "TopMargin=" + poSetting.Margins.Top.ToString());
  363. text1 = AppendToStr(text1, "RightMargin=" + poSetting.Margins.Right.ToString());
  364. text1 = AppendToStr(text1, "BottomMargin=" + poSetting.Margins.Bottom.ToString());
  365. text1 = AppendToStr(text1, "PaperSize=" + PaperSizeToStr(poSetting.PaperSize));
  366. text1 = AppendToStr(text1, "Landscape=" + (poSetting.Landscape ? "Yes" : "No"));
  367. return AppendToStr(text1, "PaperSource=" + poSetting.PaperSource.SourceName);
  368. }
  369. public static string PaperSizeToStr(PaperSize poPaper)
  370. {
  371. return JoinString(poPaper.Kind.ToString(), poPaper.Width.ToString(), poPaper.Height.ToString());
  372. }
  373. /// <summary>
  374. /// 将 几个对象的字符以 [],[],[],[] 格式连接起来
  375. /// </summary>
  376. /// <param name="poFirst"></param>
  377. /// <param name="poSecond"></param>
  378. /// <param name="poThird"></param>
  379. /// <param name="poFourth"></param>
  380. /// <returns></returns>
  381. public static string JoinString(object poFirst, object poSecond, object poThird, object poFourth)
  382. {
  383. return string.Format("[{0}],[{1}], [{2}], [{3}]", new object[] { poFirst.ToString().Trim(), poSecond.ToString().Trim(), poThird.ToString().Trim(), poFourth.ToString().Trim() });
  384. }
  385. /// <summary>
  386. /// 将 几个对象的字符以 [],[],[],[] 格式连接起来
  387. /// </summary>
  388. /// <param name="poFirst"></param>
  389. /// <param name="poSecond"></param>
  390. /// <param name="poThird"></param>
  391. /// <returns></returns>
  392. public static string JoinString(object poFirst, object poSecond, object poThird)
  393. {
  394. return string.Format("[{0}],[{1}], [{2}]", poFirst.ToString().Trim(), poSecond.ToString().Trim(), poThird.ToString().Trim());
  395. }
  396. public static string fontToStr(Font font)
  397. {
  398. string text1 = "Name:" + font.Name + ",Size:" + font.Size.ToString();
  399. if (font.Underline)
  400. {
  401. text1 = text1 + ",Underline:Y";
  402. }
  403. if (font.Italic)
  404. {
  405. text1 = text1 + ",Italic:Y";
  406. }
  407. if (font.Strikeout)
  408. {
  409. text1 = text1 + ",Strikeout:Y";
  410. }
  411. if (font.Bold)
  412. {
  413. text1 = text1 + ",Bold:Y";
  414. }
  415. return text1;
  416. }
  417. /// <summary>
  418. /// 字符串转换成 Font 型
  419. /// </summary>
  420. /// <param name="str">传入字符串</param>
  421. /// <returns>返回的font</returns>
  422. ///
  423. public static Font fontFromStr(string str)
  424. {
  425. Array array1 = StrToArray(str);
  426. Hashtable hashtable1 = new Hashtable();
  427. for (int num1 = 0; num1 < array1.Length; num1++)
  428. {
  429. Array array2 = StrToArrayEx(array1.GetValue(num1).ToString(), ":");
  430. if (array2.Length == 2)
  431. {
  432. hashtable1.Add(array2.GetValue(0), array2.GetValue(1));
  433. }
  434. }
  435. string text1 = FontFamily.GenericSansSerif.Name;
  436. if (hashtable1.ContainsKey("Name"))
  437. {
  438. text1 = hashtable1["Name"].ToString();
  439. }
  440. float single1 = 8f;
  441. if (hashtable1.ContainsKey("Size"))
  442. {
  443. single1 = (float)Utils.ValD(hashtable1["Size"].ToString());
  444. }
  445. FontStyle style1 = FontStyle.Regular;
  446. if (hashtable1.ContainsKey("Bold"))
  447. {
  448. string text2 = hashtable1["Bold"].ToString();
  449. if (text2 == "Y")
  450. {
  451. style1 |= FontStyle.Bold;
  452. }
  453. }
  454. if (hashtable1.ContainsKey("Italic"))
  455. {
  456. string text3 = hashtable1["Italic"].ToString();
  457. if (text3 == "Y")
  458. {
  459. style1 |= FontStyle.Italic;
  460. }
  461. }
  462. if (hashtable1.ContainsKey("Underline"))
  463. {
  464. string text4 = hashtable1["Underline"].ToString();
  465. if (text4 == "Y")
  466. {
  467. style1 |= FontStyle.Underline;
  468. }
  469. }
  470. if (hashtable1.ContainsKey("Strikeout"))
  471. {
  472. string text5 = hashtable1["Strikeout"].ToString();
  473. if (text5 == "Y")
  474. {
  475. style1 |= FontStyle.Strikeout;
  476. }
  477. }
  478. return new Font(text1, single1, style1);
  479. }
  480. public static string UAndT(object poObject)
  481. {
  482. return UAndT(poObject.ToString());
  483. }
  484. /// <summary>
  485. /// 将一个字符串转换成大写
  486. /// </summary>
  487. /// <param name="pcStr"></param>
  488. /// <returns></returns>
  489. public static string UAndT(string pcStr)
  490. {
  491. if (pcStr == null)
  492. {
  493. pcStr = "";
  494. }
  495. return pcStr.ToUpper().Trim();
  496. }
  497. /// <summary>
  498. /// 111,22,333;aaa,vbb,dssa
  499. /// </summary>
  500. /// <param name="pcKeys"></param>
  501. /// <param name="pcValues"></param>
  502. /// <returns></returns>
  503. public static Hashtable StrToHashtable(string pcKeys, string pcValues)
  504. {
  505. Hashtable hashtable1 = null;
  506. Array array1 = StrToArray(pcKeys);
  507. Array array2 = StrToArray(pcValues);
  508. if (array1.Length == array2.Length)
  509. {
  510. hashtable1 = new Hashtable();
  511. for (int i = 0; i < array1.Length; i++)
  512. {
  513. if (!hashtable1.ContainsKey(UAndT(array1.GetValue(i))))
  514. hashtable1.Add(UAndT(array1.GetValue(i)), array2.GetValue(i));
  515. }
  516. }
  517. return hashtable1;
  518. }
  519. public static Dictionary<string, object> StrToDictionary(string pcKeys, string pcValues)
  520. {
  521. Dictionary<string, object> loRetVal = null;
  522. Array array1 = StrToArray(pcKeys);
  523. Array array2 = StrToArray(pcValues);
  524. if (array1.Length == array2.Length)
  525. {
  526. loRetVal = new Dictionary<string, object>();
  527. for (int i = 0; i < array1.Length; i++)
  528. {
  529. if (!loRetVal.ContainsKey(UAndT(array1.GetValue(i))))
  530. loRetVal.Add(UAndT(array1.GetValue(i)), array2.GetValue(i));
  531. }
  532. }
  533. return loRetVal;
  534. }
  535. /// <summary>
  536. /// 如果 传人的字符 为 TRUE、YES 或者 Y ,1 返回 true。
  537. /// </summary>
  538. /// <param name="pcStr"></param>
  539. /// <returns></returns>
  540. public static bool StrToBool(string pcStr)
  541. {
  542. bool lbRetVal = false;
  543. pcStr = UAndT(pcStr);
  544. if (((pcStr != "TRUE") && (pcStr != "YES")) && (pcStr != "Y") && (pcStr != "1"))
  545. {
  546. return lbRetVal;
  547. }
  548. return true;
  549. }
  550. public static string GetItemFromStrEx(string pcStr, int piIndex, string separator)
  551. {
  552. string lcRetVal = "";
  553. Array loArr = StrToArrayEx(pcStr, separator);
  554. if ((piIndex < loArr.Length) && (piIndex >= 0))
  555. {
  556. lcRetVal = loArr.GetValue(piIndex).ToString();
  557. }
  558. return lcRetVal;
  559. }
  560. public static string FormatStr(string pcString)
  561. {
  562. return pcString.Replace("[", "").Replace("]", "").Trim();
  563. }
  564. public static string StrFromObj(object poObj)
  565. {
  566. if (poObj == null)
  567. {
  568. return "";
  569. }
  570. return poObj.ToString();
  571. }
  572. }
  573. }