字符串帮助类
View Code
1 /// <summary>
2 /// 关于字符串的公共方法
3 /// </summary>
4 public class StringHelper
5 {
6 #region "截取字符串"
7 /// <summary>
8 /// 截取字符串
9 /// </summary>
10 /// <param name="str"></param>
11 /// <param name="length"></param>
12 /// <returns></returns>
13 public static string SubString(string str, int length)
14 {
15 if (length >= str.Length)
16 {
17 length = str.Length;
18 }
19 return str.Substring(0, length);
20 }
21
22 /// <summary>
23 /// 从第几位截取几个字符串
24 /// </summary>
25 /// <param name="str"></param>
26 /// <param name="begin"></param>
27 /// <param name="length"></param>
28 /// <returns></returns>
29 public static string SubString(string str, int begin, int length)
30 {
31 if (str.Length < begin)
32 {
33 return "";
34 }
35 if (length >= str.Length - begin)
36 {
37 length = str.Length - begin;
38 }
39 return str.Substring(begin, length);
40 }
41
42 /// <summary>
43 /// 截取字符串并且在尾部添加需要的内容
44 /// </summary>
45 /// <param name="str"></param>
46 /// <param name="length"></param>
47 /// <param name="appendCode"></param>
48 /// <returns></returns>
49 public static string SubStringAndAppend(string str, int length, string appendCode)
50 {
51 return SubString(str, length) + appendCode;
52 }
53 /// <summary>
54 /// 截取HTML n个字符的数据
55 /// </summary>
56 /// <param name="htmlString"></param>
57 /// <param name="length"></param>
58 /// <param name="appendCode"></param>
59 /// <returns></returns>
60 public static string SubStringHTML(string htmlString, int length, string appendCode)
61 {
62 MatchCollection matchCollection = null;
63 StringBuilder resultString = new StringBuilder();
64 int charLength = 0;
65 char charTemp;
66 bool isHtmlMark = false;
67 bool isHtmlCode = false;
68 char[] pChars = htmlString.ToCharArray();
69
70 for (int i = 0; i < pChars.Length; i++)
71 {
72 charTemp = pChars[i];
73
74 if (charTemp.Equals("<"))
75 {
76 isHtmlMark = true;
77 }
78 else if (charTemp.Equals("&"))
79 {
80 isHtmlCode = true;
81 }
82 else if (charTemp.Equals(">") && isHtmlMark)
83 {
84 charLength--;
85 isHtmlMark = false;
86 }
87 else if (charTemp.Equals(";") && isHtmlCode)
88 {
89 isHtmlCode = false;
90 }
91
92 if (!isHtmlCode && !isHtmlMark)
93 {
94 charLength++;
95 if (System.Text.Encoding.Default.GetBytes(charTemp + "").Length > 1)
96 {
97 charLength++;
98 }
99 }
100
101 resultString.Append(charTemp);
102 if (charLength >= length)
103 {
104 break;
105 }
106 }
107
108 resultString.Append(appendCode);
109 //取出截取字符串中的HTML标记
110 string temp_result = Regex.Replace(resultString.ToString(), "(>)[^<>]*(<?)", "$1$2", RegexOptions.IgnoreCase);
111 //去掉不需要结素标记的HTML标记
112 temp_result = Regex.Replace(temp_result, @"</?(area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/?>", "", RegexOptions.IgnoreCase);
113 //去掉成对的HTML标记
114 temp_result = Regex.Replace(temp_result, @"<([a-zA-Z]+)[^<>]*>(.*?)</\1>", "", RegexOptions.IgnoreCase);
115 //用正则表达式取出标记
116 matchCollection = Regex.Matches(temp_result, "<([a-zA-Z]+)[^<>]*>");
117 ArrayList htmlArray = new ArrayList();
118 foreach (Match mt in matchCollection)
119 {
120 htmlArray.Add(mt.Result("$1"));
121 }
122 for (int i = htmlArray.Count - 1; i >= 0; i--)
123 {
124 resultString.Append("</");
125 resultString.Append(htmlArray[i]);
126 resultString.Append(">");
127 }
128 return resultString.ToString();
129 }
130 #endregion
131
132 #region "数据类型转换"
133
134 /// <summary>
135 /// 将字符转换成日期类型的数据
136 /// </summary>
137 /// <param name="str"></param>
138 /// <returns></returns>
139 public static DateTime StringToDateTime(string str)
140 {
141 if (ValidateHelper.IsDateTime(str))
142 {
143 return DateTime.Parse(str);
144 }
145 else
146 {
147 return DateTime.Parse("1900-1-1");
148 }
149 }
150
151 /// <summary>
152 /// 将字符串类型数据转换成Int类型数据
153 /// </summary>
154 /// <param name="str"></param>
155 /// <returns></returns>
156 public static int StringToInt(string str)
157 {
158 if (ValidateHelper.IsNumberSign(str))
159 {
160 return int.Parse(str);
161 }
162 else
163 {
164 return 0;
165 }
166 }
167
168 /// <summary>
169 /// 将字符串类型数据转换成Double类型数据
170 /// </summary>
171 /// <param name="str"></param>
172 /// <returns></returns>
173 public static double String2Double(string str)
174 {
175 if (ValidateHelper.IsNumberSign(str))
176 {
177 return double.Parse(str);
178 }
179 else
180 {
181 return 0.0;
182 }
183 }
184
185 /// <summary>
186 /// 将任何一个Object类型数据转换成string 类型数据,防止为K的数据时
187 /// </summary>
188 /// <param name="obj"></param>
189 /// <returns></returns>
190 public static string ToString(object obj)
191 {
192 if (obj != null)
193 {
194 return obj.ToString();
195 }
196 else
197 {
198 return "";
199 }
200 }
201 #endregion
202 }