![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Text;
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
namespace PmfStringPlus
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
15
/// 常用函数基类
16
/// </summary>
17
public class StringPlus
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
替换字符串#region 替换字符串
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
21
/// 功能:替换字符
22
/// </summary>
23
/// <param name="strVAlue">字符串</param>
24
/// <returns>替换掉'的字符串</returns>
25
public static string FilterSQL(string strVAlue)
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
string str = "";
28
str = strVAlue.Replace("''", "");
29
return str;
30
}
31
#endregion
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
对表 表单内容进行转换HTML操作,#region 对表 表单内容进行转换HTML操作,
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
35
/// 功能:对表 表单内容进行转换HTML操作,
36
/// </summary>
37
/// <param name="fString">html字符串</param>
38
/// <returns></returns>
39
public static string HtmlCode(string fString)
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
return str;
50
}
51
#endregion
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
判断是否:返回值:√ or ×#region 判断是否:返回值:√ or ×
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
55
/// 判断是否:返回值:√ or ×
56
/// </summary>
57
/// <param name="b">true 或false</param>
58
/// <returns>√ or ×</returns>
59
public static string Judgement(bool b)
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
截取字符串#region 截取字符串
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
int i = 0, j = 0;
81
foreach (char chr in str)
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
if ((int)chr > 127)
84![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
i += 2;
86
}
87
else
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
i++;
90
}
91
if (i > length)
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
str = str.Substring(0, j);
94
if (flg)
95
str += "![](https://www.cnblogs.com/Images/dot.gif)
";
96
break;
97
}
98
j++;
99
}
100
return str;
101
}
102
#endregion
103![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
截取字符串+…#region 截取字符串+…
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
119
if ((int)s[i] == 63)
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
intLength += 2;
122
}
123
else
124![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125
intLength += 1;
126
}
127![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128
try
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
130
strString += strInput.Substring(i, 1);
131
}
132
catch
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
break;
135
}
136![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137
if (intLength > intlen)
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
139
break;
140
}
141
}
142
//如果截过则加上半个省略号
143
byte[] mybyte = System.Text.Encoding.Default.GetBytes(strInput);
144
if (mybyte.Length > intlen)
145![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
146
strString += "…";
147
}
148
return strString;
149
}
150
#endregion
151![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字符串分函数#region 字符串分函数
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
string[] s = strings.Split(char.Parse(Separ));
163
return s[index];
164
}
165
#endregion
166![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
分解字符串为数组#region 分解字符串为数组
168![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
176
if (splitstr != "")
177![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
178
System.Collections.ArrayList c = new System.Collections.ArrayList();
179
while (true)
180![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
181
int thissplitindex = str.IndexOf(splitstr);
182
if (thissplitindex >= 0)
183![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184
c.Add(str.Substring(0, thissplitindex));
185
str = str.Substring(thissplitindex + splitstr.Length);
186
}
187
else
188![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
196
d[i] = c[i].ToString();
197
}
198
return d;
199
}
200
else
201![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
202![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return new string[]
{ str };
203
}
204
}
205
#endregion
206![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
207![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
URL编码#region URL编码
208![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
209
/// URL编码
210
/// </summary>
211
/// <param name="str">字符串</param>
212
/// <returns></returns>
213
public static string UrlEncoding(string str)
214![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
215
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
216
return System.Text.Encoding.UTF8.GetString(bytes).ToString();
217
}
218
#endregion
219![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
220![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
获取Web.config中的配置字段值#region 获取Web.config中的配置字段值
221![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
222
/// 获取全局配置参数
223
/// </summary>
224
/// <param name="key">键名</param>
225
/// <returns>参数</returns>
226
public static string GetApp(string key)
227![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
228
System.Configuration.AppSettingsReader appr = new System.Configuration.AppSettingsReader();
229
try
230![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
231
string str = (string)appr.GetValue(key, typeof(string));
232
if (str == null || str == "") return null;
233
return str;
234
}
235![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch (Exception E)
{ }
236
return null;
237
}
238![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
239
#endregion
240![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
241![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
根据传入的字符串是否为yes/no返回Bit#region 根据传入的字符串是否为yes/no返回Bit
242![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
243
/// 根据传入的字符串是否为yes/no返回Bit
244
/// </summary>
245
/// <param name="flg"></param>
246
/// <returns></returns>
247
public static int GetBitBool(string flg)
248![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
249
int str = 0;
250
switch (flg.ToLower())
251![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
265![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Html编码#region Html编码
266![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
267
/// HTML编码
268
/// </summary>
269
/// <param name="strInput"></param>
270
/// <returns></returns>
271
public static string HtmlEncode(string strInput)
272![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
273
string str;
274
try
275![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
276
str = HttpContext.Current.Server.HtmlEncode(strInput);
277
}
278
catch
279![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
280
str = "error";
281
}
282
return str;
283
}
284![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
285
/// HTML解码
286
/// </summary>
287
/// <param name="strInput"></param>
288
/// <returns></returns>
289
public static string HtmlDecode(string strInput)
290![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
291
string str;
292
try
293![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
294
str = HttpContext.Current.Server.HtmlDecode(strInput);
295
}
296
catch
297![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
298
str = "error";
299
}
300
return str;
301
}
302
#endregion
303![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
304![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false#region 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
305![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
313
bool Rev = true;
314
string chr;
315
if (aimString == "" || aimString == null) return false;
316
for (int i = 0; i < aimString.Length; i++)
317![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
318
chr = aimString.Substring(i, 1);
319
if (srcString.IndexOf(chr) < 0)
320![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
321
return false;
322
break;
323
}
324![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
325
}
326
return Rev;
327
}
328
#endregion
329![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
330![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
检测字符串中是否含有中文及中文长度#region 检测字符串中是否含有中文及中文长度
331![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
332
/// 检测字符串中是否含有中文及中文长度
333
/// </summary>
334
/// <param name="str">要检测的字符串</param>
335
/// <returns>中文字符串长度</returns>
336
public static int CnStringLength(string str)
337![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
343
if (b[i] == 63) //判断是否为汉字或全脚符号
344![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
345
l++;
346
}
347
}
348
return l;
349
350
}
351
#endregion
352![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
353![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
取字符串右侧的几个字符#region 取字符串右侧的几个字符
354![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
362
string Rev = "";
363![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
364
if (str.Length < length)
365![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
366
Rev = str;
367![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
368
}
369
else
370![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
371
Rev = str.Substring(str.Length - length, length);
372
}
373
return Rev;
374![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
375![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
376
}
377
#endregion
378![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
379![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
替换右侧的字符串#region 替换右侧的字符串
380![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
381![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
390![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
391
string Rev = "";
392
if (GetStrRight(str, strsrc.Length) != strsrc)
393![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
394
Rev = str;
395
}
396
else
397![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
398
Rev = str.Substring(0, str.Length - strsrc.Length).ToString() + straim.ToString();
399
}
400
return Rev;
401
}
402
#endregion
403
}
404![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
405
}
406![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2008-03-23 11:51
飘渺峰
阅读(
604)
评论()
编辑
收藏
举报