123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- using System;
- using System.Drawing;
- using System.IO;
- using System.Drawing.Imaging;
- using System.Collections;
- using System.Drawing.Printing;
- using System.Collections.Generic;
- using System.Globalization;
- namespace SysBaseLibs
- {
- public class UtilStr
- {
- /// <summary>
- /// 得到该对象的字符串,在字符串的两边加上“[”“]”
- /// </summary>
- /// <param name="poObject">处理的对象</param>
- /// <returns>字符串</returns>
- public static string GetDelimitedStr(object poObject)
- {
- return ("[" + poObject.ToString() + "]");
- }
- /// <summary>
- /// 连接连个对象的字符串型,并把他们连接成一个字符串返回出来。
- /// </summary>
- /// <param name="poFirst"></param>
- /// <param name="poSecond"></param>
- /// <returns></returns>
- public static string JoinString(object poFirst, object poSecond)
- {
- return string.Format("[{0}],[{1}]", poFirst.ToString().Trim(), poSecond.ToString().Trim());
- }
- /// <summary>
- /// 将一个对象以字符串形式与另外一个字符串进行连接,并添加“[”,“]”标识符号
- /// </summary>
- /// <param name="pcSource">被连接的字符串</param>
- /// <param name="poTobeAppended">连接的字符串</param>
- /// <returns>返回值</returns>
- public static string AddStr(string pcSource, object poTobeAppended)
- {
- string lcStr = GetDelimitedStr(poTobeAppended);
- pcSource = pcSource.Trim();
- if (pcSource == "")
- {
- return lcStr;
- }
- if ((pcSource[0] == '[') && (pcSource[pcSource.Length - 1] == ']'))
- {
- return (pcSource + string.Format(",[{0}]", poTobeAppended.ToString().Trim()));
- }
- return JoinString(pcSource, poTobeAppended);
- }
- /// <summary>
- /// 直接连接两个字符串
- /// </summary>
- /// <param name="pcSource"></param>
- /// <param name="poTobeAppended"></param>
- /// <returns></returns>
- public static string AppendToStr(string pcSource, object poTobeAppended)
- {
- string lcStr = GetDelimitedStr(poTobeAppended);
- pcSource = pcSource.Trim();
- if (pcSource != "")
- {
- lcStr = pcSource + "," + lcStr;
- }
- return lcStr;
- }
- /// <summary>
- /// 将一个Array转换成字符串,默认分割符为","
- /// </summary>
- /// <param name="paStr"></param>
- /// <returns></returns>
- public static string ArrayToStr(Array paStr)
- {
- return ArrayToStrEx(paStr, ",");
- }
- /// <summary>
- /// 根据传入的分割符号将Array 转换成字符串
- /// </summary>
- /// <param name="paStr"></param>
- /// <param name="separator"></param>
- /// <returns></returns>
- public static string ArrayToStrEx(Array paStr, string pcSeparator)
- {
- string lcStr = "";
- foreach (string lcStr2 in paStr)
- {
- lcStr = lcStr + ((lcStr == "") ? "" : pcSeparator) + GetDelimitedStr(lcStr2);
- }
- return lcStr;
- }
- /// <summary>
- /// 将字符串转换成Array 对象 ,以","为分割符。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static Array StrToArray(string str)
- {
- return StrToArrayEx(str, ",");
- }
- /// <summary>
- /// 将一个字符串以分割符转换成Array对象,如 a,[b,[c,d]],e 将分割成 a ; b,[c,d] ; e 三个串
- /// </summary>
- /// <param name="str"></param>
- /// <param name="separator"></param>
- /// <returns></returns>
- public static Array StrToArrayEx(string str, string separator)
- {
- ArrayList list1 = new ArrayList();
- str = (str == null) ? "" : str.Trim();
- int num1 = separator.Length;
- while (str.Length > 0)
- {
- int num2 = str.IndexOf(separator);
- int num3 = str.IndexOf("[");
- if ((num3 >= 0) && (num3 < num2))
- {
- int num4 = 1;
- int num5 = str.Length;
- int num6 = num3 + 1;
- while (num6 < num5)
- {
- switch (str[num6])
- {
- case '[':
- num4++;
- goto Label_0082;
- case '\\':
- goto Label_0082;
- case ']':
- break;
- default:
- goto Label_0082;
- }
- num4--;
- Label_0082:
- if (num4 == 0)
- {
- break;
- }
- num6++;
- }
- num2 = str.IndexOf(separator, num6);
- }
- if (num2 < 0)
- {
- num2 = str.Length;
- }
- string text1 = str.Substring(0, num2);
- int num7 = text1.Length;
- if (((num7 > 0) && (text1[0] == '[')) && (text1[num7 - 1] == ']'))
- {
- text1 = text1.Substring(1, num7 - 2);
- }
- list1.Add(text1.Trim());
- if ((num2 + num1) > str.Length)
- {
- str = "";
- }
- else
- {
- str = str.Substring(num2 + num1).Trim();
- }
- }
- Array array1 = Array.CreateInstance(typeof(string), list1.Count);
- list1.CopyTo(array1);
- return array1;
- }
- /// <summary>
- /// 将字符串转换成 Color 对象
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static Color colorFromStr(string str)
- {
- Color color1 = Color.Black;
- Array array1 = StrToArray(str);
- int num1 = 0;
- int num2 = 0;
- int num3 = 0;
- if (array1.Length >= 3)
- {
- num1 = (int)Utils.ValD((string)array1.GetValue(0));
- num2 = (int)Utils.ValD((string)array1.GetValue(1));
- num3 = (int)Utils.ValD((string)array1.GetValue(2));
- color1 = Color.FromArgb(num1, num2, num3);
- }
- return color1;
- }
- /// <summary>
- /// Translate Color to String
- /// </summary>
- /// <param name="color"></param>
- /// <returns></returns>
- public static string colorToStr(Color color)
- {
- return (color.R.ToString() + "," + color.G.ToString() + "," + color.B.ToString());
- }
- /// <summary>
- /// 从pcSource 末尾移除 pcRemoveStr
- /// </summary>
- /// <param name="pcSource"></param>
- /// <param name="pcRemoveStr"></param>
- /// <returns></returns>
- public static string TrimEnd(string pcSource, string pcRemoveStr)
- {
- char[] chArray1 = pcRemoveStr.ToCharArray();
- return pcSource.TrimEnd(chArray1);
- }
- /// <summary>
- /// 字符串转换成日期格式
- /// </summary>
- /// <param name="pcStr"></param>
- /// <returns></returns>
- public static DateTime StrToDt(string pcStr)
- {
- DateTime time1 = new DateTime(1900, 1, 1);
- try
- {
- return DateTime.Parse(pcStr);
- }
- catch (Exception)
- {
- }
- return new DateTime(1900, 1, 1);
- }
- /// <summary>
- /// 字符串转换成日期格式
- /// </summary>
- /// <param name="pcStr"></param>
- /// <returns></returns>
- public static DateTime StrToDt(string pcStr,string format)
- {
- DateTime time1 = new DateTime(1900, 1, 1);
- try
- {
- DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
- dtFormat.ShortDatePattern = format;
- //DateTime dt = DateTime.ParseExact(pcStr, format, System.Globalization.CultureInfo.CurrentCulture);
- DateTime dt = Convert.ToDateTime(pcStr, dtFormat);//DateTime dt
- return dt;
- }
- catch (Exception err)
- {
- ThreadLog.LogException(err);
- }
- return new DateTime(1900, 1, 1);
- }
- /// <summary>
- /// 将 piCount 个 pcString 串成一个字符串
- /// </summary>
- /// <param name="pcString"></param>
- /// <param name="piCount"></param>
- /// <returns></returns>
- public static string Replicate(string pcString, int piCount)
- {
- string text1 = "";
- for (int i = 0; i < piCount; i++)
- {
- text1 = text1 + pcString;
- }
- return text1;
- }
- /// <summary>
- /// Translate RectangleF to string
- /// </summary>
- /// <param name="rect"></param>
- /// <returns></returns>
- public static string rectToStr(RectangleF rect)
- {
- return (rect.X.ToString() + "," + rect.Y.ToString() + "," + rect.Width.ToString() + "," + rect.Height.ToString());
- }
- /// <summary>
- /// Translate string to RectangleF
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static RectangleF rectFromStr(string str)
- {
- Array array1 = StrToArray(str);
- RectangleF ef1 = (RectangleF)new Rectangle(0, 0, 0, 0);
- if (array1.Length >= 4)
- {
- ef1.X = (float)Utils.ValD((string)array1.GetValue(0));
- ef1.Y = (float)Utils.ValD((string)array1.GetValue(1));
- ef1.Width = (float)Utils.ValD((string)array1.GetValue(2));
- ef1.Height = (float)Utils.ValD((string)array1.GetValue(3));
- }
- return ef1;
- }
- /// <summary>
- /// PrinterSettings To String
- /// </summary>
- /// <param name="poSetting"></param>
- /// <returns></returns>
- public static string PrinterSettingToStr(PrinterSettings poSetting)
- {
- string text1 = "";
- text1 = AppendToStr(text1, "PrinterName=" + poSetting.PrinterName);
- text1 = AppendToStr(text1, "FromPage=" + poSetting.FromPage.ToString());
- text1 = AppendToStr(text1, "ToPage=" + poSetting.ToPage.ToString());
- return AppendToStr(text1, "Copies=" + poSetting.Copies.ToString());
- }
- /// <summary>
- /// String To PrinterSettings
- /// </summary>
- /// <param name="pcString"></param>
- /// <returns></returns>
- public static PrinterSettings PrinterSettingFromStr(string pcString)
- {
- PrinterSettings settings1 = new PrinterSettings();
- Array array1 = StrToArray(pcString);
- foreach (string text1 in array1)
- {
- Array array2 = StrToArrayEx(text1, "=");
- if (array2.Length == 2)
- {
- string text4;
- string text2 = array2.GetValue(0).ToString();
- string text3 = array2.GetValue(1).ToString();
- if ((text4 = text2) != null)
- {
- text4 = string.IsInterned(text4);
- if (text4 == "PrinterName")
- {
- settings1.PrinterName = text3;
- }
- else
- {
- if (text4 == "FromPage")
- {
- settings1.FromPage = Utils.ValI(text3);
- continue;
- }
- if (text4 == "ToPage")
- {
- settings1.ToPage = Utils.ValI(text3);
- continue;
- }
- if (text4 == "Copies")
- {
- settings1.Copies = (short)Utils.ValI(text3);
- }
- }
- }
- }
- }
- return settings1;
- }
- public static string PrinterPageSettingToStr(PrinterSettings poPrinter, PageSettings poPage)
- {
- if (poPage == null)
- {
- return ("[" + PrinterSettingToStr(poPrinter) + "],[Default]");
- }
- return ("[" + PrinterSettingToStr(poPrinter) + "],[" + PageSettingToStr(poPage) + "]");
- }
- public static string PageSettingToStr(PageSettings poSetting)
- {
- string text1 = "";
- text1 = AppendToStr(text1, "LeftMargin=" + poSetting.Margins.Left.ToString());
- text1 = AppendToStr(text1, "TopMargin=" + poSetting.Margins.Top.ToString());
- text1 = AppendToStr(text1, "RightMargin=" + poSetting.Margins.Right.ToString());
- text1 = AppendToStr(text1, "BottomMargin=" + poSetting.Margins.Bottom.ToString());
- text1 = AppendToStr(text1, "PaperSize=" + PaperSizeToStr(poSetting.PaperSize));
- text1 = AppendToStr(text1, "Landscape=" + (poSetting.Landscape ? "Yes" : "No"));
- return AppendToStr(text1, "PaperSource=" + poSetting.PaperSource.SourceName);
- }
- public static string PaperSizeToStr(PaperSize poPaper)
- {
- return JoinString(poPaper.Kind.ToString(), poPaper.Width.ToString(), poPaper.Height.ToString());
- }
- /// <summary>
- /// 将 几个对象的字符以 [],[],[],[] 格式连接起来
- /// </summary>
- /// <param name="poFirst"></param>
- /// <param name="poSecond"></param>
- /// <param name="poThird"></param>
- /// <param name="poFourth"></param>
- /// <returns></returns>
- public static string JoinString(object poFirst, object poSecond, object poThird, object poFourth)
- {
- return string.Format("[{0}],[{1}], [{2}], [{3}]", new object[] { poFirst.ToString().Trim(), poSecond.ToString().Trim(), poThird.ToString().Trim(), poFourth.ToString().Trim() });
- }
- /// <summary>
- /// 将 几个对象的字符以 [],[],[],[] 格式连接起来
- /// </summary>
- /// <param name="poFirst"></param>
- /// <param name="poSecond"></param>
- /// <param name="poThird"></param>
- /// <returns></returns>
- public static string JoinString(object poFirst, object poSecond, object poThird)
- {
- return string.Format("[{0}],[{1}], [{2}]", poFirst.ToString().Trim(), poSecond.ToString().Trim(), poThird.ToString().Trim());
- }
- public static string fontToStr(Font font)
- {
- string text1 = "Name:" + font.Name + ",Size:" + font.Size.ToString();
- if (font.Underline)
- {
- text1 = text1 + ",Underline:Y";
- }
- if (font.Italic)
- {
- text1 = text1 + ",Italic:Y";
- }
- if (font.Strikeout)
- {
- text1 = text1 + ",Strikeout:Y";
- }
- if (font.Bold)
- {
- text1 = text1 + ",Bold:Y";
- }
- return text1;
- }
- /// <summary>
- /// 字符串转换成 Font 型
- /// </summary>
- /// <param name="str">传入字符串</param>
- /// <returns>返回的font</returns>
- ///
- public static Font fontFromStr(string str)
- {
- Array array1 = StrToArray(str);
- Hashtable hashtable1 = new Hashtable();
- for (int num1 = 0; num1 < array1.Length; num1++)
- {
- Array array2 = StrToArrayEx(array1.GetValue(num1).ToString(), ":");
- if (array2.Length == 2)
- {
- hashtable1.Add(array2.GetValue(0), array2.GetValue(1));
- }
- }
- string text1 = FontFamily.GenericSansSerif.Name;
- if (hashtable1.ContainsKey("Name"))
- {
- text1 = hashtable1["Name"].ToString();
- }
- float single1 = 8f;
- if (hashtable1.ContainsKey("Size"))
- {
- single1 = (float)Utils.ValD(hashtable1["Size"].ToString());
- }
- FontStyle style1 = FontStyle.Regular;
- if (hashtable1.ContainsKey("Bold"))
- {
- string text2 = hashtable1["Bold"].ToString();
- if (text2 == "Y")
- {
- style1 |= FontStyle.Bold;
- }
- }
- if (hashtable1.ContainsKey("Italic"))
- {
- string text3 = hashtable1["Italic"].ToString();
- if (text3 == "Y")
- {
- style1 |= FontStyle.Italic;
- }
- }
- if (hashtable1.ContainsKey("Underline"))
- {
- string text4 = hashtable1["Underline"].ToString();
- if (text4 == "Y")
- {
- style1 |= FontStyle.Underline;
- }
- }
- if (hashtable1.ContainsKey("Strikeout"))
- {
- string text5 = hashtable1["Strikeout"].ToString();
- if (text5 == "Y")
- {
- style1 |= FontStyle.Strikeout;
- }
- }
- return new Font(text1, single1, style1);
- }
- public static string UAndT(object poObject)
- {
- return UAndT(poObject.ToString());
- }
- /// <summary>
- /// 将一个字符串转换成大写
- /// </summary>
- /// <param name="pcStr"></param>
- /// <returns></returns>
- public static string UAndT(string pcStr)
- {
- if (pcStr == null)
- {
- pcStr = "";
- }
- return pcStr.ToUpper().Trim();
- }
- /// <summary>
- /// 111,22,333;aaa,vbb,dssa
- /// </summary>
- /// <param name="pcKeys"></param>
- /// <param name="pcValues"></param>
- /// <returns></returns>
- public static Hashtable StrToHashtable(string pcKeys, string pcValues)
- {
- Hashtable hashtable1 = null;
- Array array1 = StrToArray(pcKeys);
- Array array2 = StrToArray(pcValues);
- if (array1.Length == array2.Length)
- {
- hashtable1 = new Hashtable();
- for (int i = 0; i < array1.Length; i++)
- {
- if (!hashtable1.ContainsKey(UAndT(array1.GetValue(i))))
- hashtable1.Add(UAndT(array1.GetValue(i)), array2.GetValue(i));
- }
- }
- return hashtable1;
- }
- public static Dictionary<string, object> StrToDictionary(string pcKeys, string pcValues)
- {
- Dictionary<string, object> loRetVal = null;
- Array array1 = StrToArray(pcKeys);
- Array array2 = StrToArray(pcValues);
- if (array1.Length == array2.Length)
- {
- loRetVal = new Dictionary<string, object>();
- for (int i = 0; i < array1.Length; i++)
- {
- if (!loRetVal.ContainsKey(UAndT(array1.GetValue(i))))
- loRetVal.Add(UAndT(array1.GetValue(i)), array2.GetValue(i));
- }
- }
- return loRetVal;
- }
- /// <summary>
- /// 如果 传人的字符 为 TRUE、YES 或者 Y ,1 返回 true。
- /// </summary>
- /// <param name="pcStr"></param>
- /// <returns></returns>
- public static bool StrToBool(string pcStr)
- {
- bool lbRetVal = false;
- pcStr = UAndT(pcStr);
- if (((pcStr != "TRUE") && (pcStr != "YES")) && (pcStr != "Y") && (pcStr != "1"))
- {
- return lbRetVal;
- }
- return true;
- }
- public static string GetItemFromStrEx(string pcStr, int piIndex, string separator)
- {
- string lcRetVal = "";
- Array loArr = StrToArrayEx(pcStr, separator);
- if ((piIndex < loArr.Length) && (piIndex >= 0))
- {
- lcRetVal = loArr.GetValue(piIndex).ToString();
- }
- return lcRetVal;
- }
- public static string FormatStr(string pcString)
- {
- return pcString.Replace("[", "").Replace("]", "").Trim();
- }
- public static string StrFromObj(object poObj)
- {
- if (poObj == null)
- {
- return "";
- }
- return poObj.ToString();
- }
- }
- }
|