Code
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Text;
11
12namespace PmfStringPlus
13{
14 /**//// <summary>
15 /// 常用函数基类
16 /// </summary>
17 public class StringPlus
18 {
19 替换字符串#region 替换字符串
20 /**//// <summary>
21 /// 功能:替换字符
22 /// </summary>
23 /// <param name="strVAlue">字符串</param>
24 /// <returns>替换掉'的字符串</returns>
25 public static string FilterSQL(string strVAlue)
26 {
27 string str = "";
28 str = strVAlue.Replace("''", "");
29 return str;
30 }
31 #endregion
32
33 对表 表单内容进行转换HTML操作,#region 对表 表单内容进行转换HTML操作,
34 /**//// <summary>
35 /// 功能:对表 表单内容进行转换HTML操作,
36 /// </summary>
37 /// <param name="fString">html字符串</param>
38 /// <returns></returns>
39 public static string HtmlCode(string fString)
40 {
41 string str = "";
42 str = fString.Replace(">", ">");
43 str = fString.Replace("<", "<");
44 str = fString.Replace(" ", " ");
45 str = fString.Replace("\n", "<br />");
46 str = fString.Replace("\r", "<br />");
47 str = fString.Replace("\r\n", "<br />");
48
49 return str;
50 }
51#endregion
52
53 判断是否:返回值:√ or ×#region 判断是否:返回值:√ or ×
54 /**//// <summary>
55 /// 判断是否:返回值:√ or ×
56 /// </summary>
57 /// <param name="b">true 或false</param>
58 /// <returns>√ or ×</returns>
59 public static string Judgement(bool b)
60 {
61 string s = "";
62 if (b == true)
63 s = "<b><font color=#009900>√</font></b>";
64 else
65 s = "<b><font color=#FF0000>×</font></b>";
66 return s;
67 }
68 #endregion
69
70 截取字符串#region 截取字符串
71 /**//// <summary>
72 /// 功能:截取字符串长度
73 /// </summary>
74 /// <param name="str">要截取的字符串</param>
75 /// <param name="length">字符串长度</param>
76 /// <param name="flg">true:加,flase:不加</param>
77 /// <returns></returns>
78 public static string GetString(string str, int length, bool flg)
79 {
80 int i = 0, j = 0;
81 foreach (char chr in str)
82 {
83 if ((int)chr > 127)
84 {
85 i += 2;
86 }
87 else
88 {
89 i++;
90 }
91 if (i > length)
92 {
93 str = str.Substring(0, j);
94 if (flg)
95 str += "";
96 break;
97 }
98 j++;
99 }
100 return str;
101 }
102 #endregion
103
104 截取字符串+…#region 截取字符串+…
105 /**//// <summary>
106 /// 截取字符串+…
107 /// </summary>
108 /// <param name="strInput"></param>
109 /// <param name="intlen"></param>
110 /// <returns></returns>
111 public static string CutString(string strInput, int intlen)//截取字符串
112 {
113 ASCIIEncoding ascii = new ASCIIEncoding();
114 int intLength = 0;
115 string strString = "";
116 byte[] s = ascii.GetBytes(strInput);
117 for (int i = 0; i < s.Length; i++)
118 {
119 if ((int)s[i] == 63)
120 {
121 intLength += 2;
122 }
123 else
124 {
125 intLength += 1;
126 }
127
128 try
129 {
130 strString += strInput.Substring(i, 1);
131 }
132 catch
133 {
134 break;
135 }
136
137 if (intLength > intlen)
138 {
139 break;
140 }
141 }
142 //如果截过则加上半个省略号
143 byte[] mybyte = System.Text.Encoding.Default.GetBytes(strInput);
144 if (mybyte.Length > intlen)
145 {
146 strString += "…";
147 }
148 return strString;
149 }
150 #endregion
151
152 字符串分函数#region 字符串分函数
153 /**//// <summary>
154 /// 字符串分函数
155 /// </summary>
156 /// <param name="strID"></param>
157 /// <param name="index"></param>
158 /// <param name="Separ"></param>
159 /// <returns></returns>
160 public string StringSplit(string strings, int index, string Separ)
161 {
162 string[] s = strings.Split(char.Parse(Separ));
163 return s[index];
164 }
165 #endregion
166
167 分解字符串为数组#region 分解字符串为数组
168 /**//// <summary>
169 /// 字符串分函数
170 /// </summary>
171 /// <param name="str">要分解的字符串</param>
172 /// <param name="splitstr">分割符,可以为string类型</param>
173 /// <returns>字符数组</returns>
174 public static string[] splitstr(string str, string splitstr)
175 {
176 if (splitstr != "")
177 {
178 System.Collections.ArrayList c = new System.Collections.ArrayList();
179 while (true)
180 {
181 int thissplitindex = str.IndexOf(splitstr);
182 if (thissplitindex >= 0)
183 {
184 c.Add(str.Substring(0, thissplitindex));
185 str = str.Substring(thissplitindex + splitstr.Length);
186 }
187 else
188 {
189 c.Add(str);
190 break;
191 }
192 }
193 string[] d = new string[c.Count];
194 for (int i = 0; i < c.Count; i++)
195 {
196 d[i] = c[i].ToString();
197 }
198 return d;
199 }
200 else
201 {
202 return new string[] { str };
203 }
204 }
205 #endregion
206
207 URL编码#region URL编码
208 /**//// <summary>
209 /// URL编码
210 /// </summary>
211 /// <param name="str">字符串</param>
212 /// <returns></returns>
213 public static string UrlEncoding(string str)
214 {
215 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
216 return System.Text.Encoding.UTF8.GetString(bytes).ToString();
217 }
218 #endregion
219
220 获取Web.config中的配置字段值#region 获取Web.config中的配置字段值
221 /**//// <summary>
222 /// 获取全局配置参数
223 /// </summary>
224 /// <param name="key">键名</param>
225 /// <returns>参数</returns>
226 public static string GetApp(string key)
227 {
228 System.Configuration.AppSettingsReader appr = new System.Configuration.AppSettingsReader();
229 try
230 {
231 string str = (string)appr.GetValue(key, typeof(string));
232 if (str == null || str == "") return null;
233 return str;
234 }
235 catch (Exception E) { }
236 return null;
237 }
238
239 #endregion
240
241 根据传入的字符串是否为yes/no返回Bit#region 根据传入的字符串是否为yes/no返回Bit
242 /**//// <summary>
243 /// 根据传入的字符串是否为yes/no返回Bit
244 /// </summary>
245 /// <param name="flg"></param>
246 /// <returns></returns>
247 public static int GetBitBool(string flg)
248 {
249 int str = 0;
250 switch (flg.ToLower())
251 {
252 case "yes":
253 str = 1;
254 break;
255 case"no":
256 str = 0;
257 break;
258 default:
259 break;
260 }
261 return str;
262 }
263 #endregion
264
265 Html编码#region Html编码
266 /**//// <summary>
267 /// HTML编码
268 /// </summary>
269 /// <param name="strInput"></param>
270 /// <returns></returns>
271 public static string HtmlEncode(string strInput)
272 {
273 string str;
274 try
275 {
276 str = HttpContext.Current.Server.HtmlEncode(strInput);
277 }
278 catch
279 {
280 str = "error";
281 }
282 return str;
283 }
284 /**//// <summary>
285 /// HTML解码
286 /// </summary>
287 /// <param name="strInput"></param>
288 /// <returns></returns>
289 public static string HtmlDecode(string strInput)
290 {
291 string str;
292 try
293 {
294 str = HttpContext.Current.Server.HtmlDecode(strInput);
295 }
296 catch
297 {
298 str = "error";
299 }
300 return str;
301 }
302 #endregion
303
304 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false#region 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
305 /**//// <summary>
306 /// 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
307 /// </summary>
308 /// <param name="srcString">原始字符串</param>
309 /// <param name="aimString">目标字符串</param>
310 /// <returns></returns>
311 public static bool IsEnglish(string srcString, string aimString)
312 {
313 bool Rev = true;
314 string chr;
315 if (aimString == "" || aimString == null) return false;
316 for (int i = 0; i < aimString.Length; i++)
317 {
318 chr = aimString.Substring(i, 1);
319 if (srcString.IndexOf(chr) < 0)
320 {
321 return false;
322 break;
323 }
324
325 }
326 return Rev;
327 }
328 #endregion
329
330 检测字符串中是否含有中文及中文长度#region 检测字符串中是否含有中文及中文长度
331 /**//// <summary>
332 /// 检测字符串中是否含有中文及中文长度
333 /// </summary>
334 /// <param name="str">要检测的字符串</param>
335 /// <returns>中文字符串长度</returns>
336 public static int CnStringLength(string str)
337 {
338 ASCIIEncoding n = new ASCIIEncoding();
339 byte[] b = n.GetBytes(str);
340 int l = 0; // l 为字符串之实际长度
341 for (int i = 0; i <= b.Length - 1; i++)
342 {
343 if (b[i] == 63) //判断是否为汉字或全脚符号
344 {
345 l++;
346 }
347 }
348 return l;
349
350 }
351 #endregion
352
353 取字符串右侧的几个字符#region 取字符串右侧的几个字符
354 /**//// <summary>
355 /// 取字符串右侧的几个字符
356 /// </summary>
357 /// <param name="str">字符串</param>
358 /// <param name="length">右侧的几个字符</param>
359 /// <returns></returns>
360 public static string GetStrRight(string str, int length)
361 {
362 string Rev = "";
363
364 if (str.Length < length)
365 {
366 Rev = str;
367
368 }
369 else
370 {
371 Rev = str.Substring(str.Length - length, length);
372 }
373 return Rev;
374
375
376 }
377 #endregion
378
379 替换右侧的字符串#region 替换右侧的字符串
380
381 /**//// <summary>
382 /// 替换右侧的字符串
383 /// </summary>
384 /// <param name="str">字符串</param>
385 /// <param name="strsrc">右侧的字符串</param>
386 /// <param name="straim">要替换为的字符串</param>
387 /// <returns></returns>
388 public static string RepStrRight(string str, string strsrc, string straim)
389 {
390
391 string Rev = "";
392 if (GetStrRight(str, strsrc.Length) != strsrc)
393 {
394 Rev = str;
395 }
396 else
397 {
398 Rev = str.Substring(0, str.Length - strsrc.Length).ToString() + straim.ToString();
399 }
400 return Rev;
401 }
402 #endregion
403 }
404
405}
406
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Text;
11
12namespace PmfStringPlus
13{
14 /**//// <summary>
15 /// 常用函数基类
16 /// </summary>
17 public class StringPlus
18 {
19 替换字符串#region 替换字符串
20 /**//// <summary>
21 /// 功能:替换字符
22 /// </summary>
23 /// <param name="strVAlue">字符串</param>
24 /// <returns>替换掉'的字符串</returns>
25 public static string FilterSQL(string strVAlue)
26 {
27 string str = "";
28 str = strVAlue.Replace("''", "");
29 return str;
30 }
31 #endregion
32
33 对表 表单内容进行转换HTML操作,#region 对表 表单内容进行转换HTML操作,
34 /**//// <summary>
35 /// 功能:对表 表单内容进行转换HTML操作,
36 /// </summary>
37 /// <param name="fString">html字符串</param>
38 /// <returns></returns>
39 public static string HtmlCode(string fString)
40 {
41 string str = "";
42 str = fString.Replace(">", ">");
43 str = fString.Replace("<", "<");
44 str = fString.Replace(" ", " ");
45 str = fString.Replace("\n", "<br />");
46 str = fString.Replace("\r", "<br />");
47 str = fString.Replace("\r\n", "<br />");
48
49 return str;
50 }
51#endregion
52
53 判断是否:返回值:√ or ×#region 判断是否:返回值:√ or ×
54 /**//// <summary>
55 /// 判断是否:返回值:√ or ×
56 /// </summary>
57 /// <param name="b">true 或false</param>
58 /// <returns>√ or ×</returns>
59 public static string Judgement(bool b)
60 {
61 string s = "";
62 if (b == true)
63 s = "<b><font color=#009900>√</font></b>";
64 else
65 s = "<b><font color=#FF0000>×</font></b>";
66 return s;
67 }
68 #endregion
69
70 截取字符串#region 截取字符串
71 /**//// <summary>
72 /// 功能:截取字符串长度
73 /// </summary>
74 /// <param name="str">要截取的字符串</param>
75 /// <param name="length">字符串长度</param>
76 /// <param name="flg">true:加,flase:不加</param>
77 /// <returns></returns>
78 public static string GetString(string str, int length, bool flg)
79 {
80 int i = 0, j = 0;
81 foreach (char chr in str)
82 {
83 if ((int)chr > 127)
84 {
85 i += 2;
86 }
87 else
88 {
89 i++;
90 }
91 if (i > length)
92 {
93 str = str.Substring(0, j);
94 if (flg)
95 str += "";
96 break;
97 }
98 j++;
99 }
100 return str;
101 }
102 #endregion
103
104 截取字符串+…#region 截取字符串+…
105 /**//// <summary>
106 /// 截取字符串+…
107 /// </summary>
108 /// <param name="strInput"></param>
109 /// <param name="intlen"></param>
110 /// <returns></returns>
111 public static string CutString(string strInput, int intlen)//截取字符串
112 {
113 ASCIIEncoding ascii = new ASCIIEncoding();
114 int intLength = 0;
115 string strString = "";
116 byte[] s = ascii.GetBytes(strInput);
117 for (int i = 0; i < s.Length; i++)
118 {
119 if ((int)s[i] == 63)
120 {
121 intLength += 2;
122 }
123 else
124 {
125 intLength += 1;
126 }
127
128 try
129 {
130 strString += strInput.Substring(i, 1);
131 }
132 catch
133 {
134 break;
135 }
136
137 if (intLength > intlen)
138 {
139 break;
140 }
141 }
142 //如果截过则加上半个省略号
143 byte[] mybyte = System.Text.Encoding.Default.GetBytes(strInput);
144 if (mybyte.Length > intlen)
145 {
146 strString += "…";
147 }
148 return strString;
149 }
150 #endregion
151
152 字符串分函数#region 字符串分函数
153 /**//// <summary>
154 /// 字符串分函数
155 /// </summary>
156 /// <param name="strID"></param>
157 /// <param name="index"></param>
158 /// <param name="Separ"></param>
159 /// <returns></returns>
160 public string StringSplit(string strings, int index, string Separ)
161 {
162 string[] s = strings.Split(char.Parse(Separ));
163 return s[index];
164 }
165 #endregion
166
167 分解字符串为数组#region 分解字符串为数组
168 /**//// <summary>
169 /// 字符串分函数
170 /// </summary>
171 /// <param name="str">要分解的字符串</param>
172 /// <param name="splitstr">分割符,可以为string类型</param>
173 /// <returns>字符数组</returns>
174 public static string[] splitstr(string str, string splitstr)
175 {
176 if (splitstr != "")
177 {
178 System.Collections.ArrayList c = new System.Collections.ArrayList();
179 while (true)
180 {
181 int thissplitindex = str.IndexOf(splitstr);
182 if (thissplitindex >= 0)
183 {
184 c.Add(str.Substring(0, thissplitindex));
185 str = str.Substring(thissplitindex + splitstr.Length);
186 }
187 else
188 {
189 c.Add(str);
190 break;
191 }
192 }
193 string[] d = new string[c.Count];
194 for (int i = 0; i < c.Count; i++)
195 {
196 d[i] = c[i].ToString();
197 }
198 return d;
199 }
200 else
201 {
202 return new string[] { str };
203 }
204 }
205 #endregion
206
207 URL编码#region URL编码
208 /**//// <summary>
209 /// URL编码
210 /// </summary>
211 /// <param name="str">字符串</param>
212 /// <returns></returns>
213 public static string UrlEncoding(string str)
214 {
215 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
216 return System.Text.Encoding.UTF8.GetString(bytes).ToString();
217 }
218 #endregion
219
220 获取Web.config中的配置字段值#region 获取Web.config中的配置字段值
221 /**//// <summary>
222 /// 获取全局配置参数
223 /// </summary>
224 /// <param name="key">键名</param>
225 /// <returns>参数</returns>
226 public static string GetApp(string key)
227 {
228 System.Configuration.AppSettingsReader appr = new System.Configuration.AppSettingsReader();
229 try
230 {
231 string str = (string)appr.GetValue(key, typeof(string));
232 if (str == null || str == "") return null;
233 return str;
234 }
235 catch (Exception E) { }
236 return null;
237 }
238
239 #endregion
240
241 根据传入的字符串是否为yes/no返回Bit#region 根据传入的字符串是否为yes/no返回Bit
242 /**//// <summary>
243 /// 根据传入的字符串是否为yes/no返回Bit
244 /// </summary>
245 /// <param name="flg"></param>
246 /// <returns></returns>
247 public static int GetBitBool(string flg)
248 {
249 int str = 0;
250 switch (flg.ToLower())
251 {
252 case "yes":
253 str = 1;
254 break;
255 case"no":
256 str = 0;
257 break;
258 default:
259 break;
260 }
261 return str;
262 }
263 #endregion
264
265 Html编码#region Html编码
266 /**//// <summary>
267 /// HTML编码
268 /// </summary>
269 /// <param name="strInput"></param>
270 /// <returns></returns>
271 public static string HtmlEncode(string strInput)
272 {
273 string str;
274 try
275 {
276 str = HttpContext.Current.Server.HtmlEncode(strInput);
277 }
278 catch
279 {
280 str = "error";
281 }
282 return str;
283 }
284 /**//// <summary>
285 /// HTML解码
286 /// </summary>
287 /// <param name="strInput"></param>
288 /// <returns></returns>
289 public static string HtmlDecode(string strInput)
290 {
291 string str;
292 try
293 {
294 str = HttpContext.Current.Server.HtmlDecode(strInput);
295 }
296 catch
297 {
298 str = "error";
299 }
300 return str;
301 }
302 #endregion
303
304 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false#region 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
305 /**//// <summary>
306 /// 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
307 /// </summary>
308 /// <param name="srcString">原始字符串</param>
309 /// <param name="aimString">目标字符串</param>
310 /// <returns></returns>
311 public static bool IsEnglish(string srcString, string aimString)
312 {
313 bool Rev = true;
314 string chr;
315 if (aimString == "" || aimString == null) return false;
316 for (int i = 0; i < aimString.Length; i++)
317 {
318 chr = aimString.Substring(i, 1);
319 if (srcString.IndexOf(chr) < 0)
320 {
321 return false;
322 break;
323 }
324
325 }
326 return Rev;
327 }
328 #endregion
329
330 检测字符串中是否含有中文及中文长度#region 检测字符串中是否含有中文及中文长度
331 /**//// <summary>
332 /// 检测字符串中是否含有中文及中文长度
333 /// </summary>
334 /// <param name="str">要检测的字符串</param>
335 /// <returns>中文字符串长度</returns>
336 public static int CnStringLength(string str)
337 {
338 ASCIIEncoding n = new ASCIIEncoding();
339 byte[] b = n.GetBytes(str);
340 int l = 0; // l 为字符串之实际长度
341 for (int i = 0; i <= b.Length - 1; i++)
342 {
343 if (b[i] == 63) //判断是否为汉字或全脚符号
344 {
345 l++;
346 }
347 }
348 return l;
349
350 }
351 #endregion
352
353 取字符串右侧的几个字符#region 取字符串右侧的几个字符
354 /**//// <summary>
355 /// 取字符串右侧的几个字符
356 /// </summary>
357 /// <param name="str">字符串</param>
358 /// <param name="length">右侧的几个字符</param>
359 /// <returns></returns>
360 public static string GetStrRight(string str, int length)
361 {
362 string Rev = "";
363
364 if (str.Length < length)
365 {
366 Rev = str;
367
368 }
369 else
370 {
371 Rev = str.Substring(str.Length - length, length);
372 }
373 return Rev;
374
375
376 }
377 #endregion
378
379 替换右侧的字符串#region 替换右侧的字符串
380
381 /**//// <summary>
382 /// 替换右侧的字符串
383 /// </summary>
384 /// <param name="str">字符串</param>
385 /// <param name="strsrc">右侧的字符串</param>
386 /// <param name="straim">要替换为的字符串</param>
387 /// <returns></returns>
388 public static string RepStrRight(string str, string strsrc, string straim)
389 {
390
391 string Rev = "";
392 if (GetStrRight(str, strsrc.Length) != strsrc)
393 {
394 Rev = str;
395 }
396 else
397 {
398 Rev = str.Substring(0, str.Length - strsrc.Length).ToString() + straim.ToString();
399 }
400 return Rev;
401 }
402 #endregion
403 }
404
405}
406