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
11
using System.Text;
12
using System.Text.RegularExpressions;
13
14
/**//// <summary>
15
/// StringFormat µÄժҪ˵Ã÷
16
/// </summary>
17
public class StringFormat
18
...{
19
²¿·Ö±äÁ¿ÉùÃ÷#region ²¿·Ö±äÁ¿ÉùÃ÷
20
private static StringBuilder outstr;
21
private static Regex objregex;
22
#endregion
23
24
Êä³ö×Ö·û´®#region Êä³ö×Ö·û´®
25
/**//// <summary>
26
/// ·µ»ØÓÃÓڱ༵Ä×Ö·û´®²¢½øÐÐHtml½âÂë
27
/// </summary>
28
/// <param name="instr">ÒªÊä³öµÄ×Ö·û´®</param>
29
/// <returns></returns>
30
public static string OutString(string instr)
31
...{
32
instr = HttpContext.Current.Server.HtmlDecode(instr);
33
instr = instr.Replace("<br />" + Environment.NewLine, Environment.NewLine);
34
return instr;
35
}
36
37
/**//// <summary>
38
/// ·µ»ØÓÃÓÚÏÔʾµÄ×Ö·û´®²¢É¾³ý³¬¹ýÏÞ¶¨×ÖÊýµÄ×Ö·û
39
/// </summary>
40
/// <param name="instr">ÒªÊä³öµÄÎı¾</param>
41
/// <param name="WordCount">ÒªÊä³öµÄ×ÖÊý</param>
42
/// <returns></returns>
43
public static string OutString(string instr, int WordCount)
44
...{
45
byte[] mybyte = System.Text.Encoding.Default.GetBytes(instr);
46
if (mybyte.Length > WordCount)
47
...{
48
outstr = new StringBuilder();
49
for (int i = 0; i < instr.Length; i++)
50
...{
51
byte[] tempByte = System.Text.Encoding.Default.GetBytes(outstr.ToString());
52
if (tempByte.Length < WordCount * 2)
53
...{
54
outstr.Append(instr.Substring(i, 1));
55
}
56
else
57
...{
58
break;
59
}
60
}
61
return outstr.ToString();
62
}
63
else
64
...{
65
return instr;
66
}
67
}
68
69
/**//// <summary>
70
/// ·µ»ØÓÃÓÚÏÔʾµÄ×Ö·û´®²¢ÓÃÊ¡ÂԺŴúÌæ³¬¹ýÏÞ¶¨×ÖÊýµÄ×Ö·û
71
/// </summary>
72
/// <param name="instr">ÒªÊä³öµÄÎı¾</param>
73
/// <param name="WordCount">ÒªÊä³öµÄ×ÖÊý</param>
74
/// <param name="Prolong">Ôö¼ÓÑÓ³¤·ûºÅ</param>
75
/// <returns></returns>
76
public static string OutString(string instr, int WordCount, bool Prolong)
77
...{
78
byte[] mybyte = System.Text.Encoding.Default.GetBytes(instr);
79
if (mybyte.Length > WordCount)
80
...{
81
outstr = new StringBuilder();
82
for (int i = 0; i < instr.Length; i++)
83
...{
84
byte[] tempByte = System.Text.Encoding.Default.GetBytes(outstr.ToString());
85
if (tempByte.Length < WordCount * 2)
86
...{
87
outstr.Append(instr.Substring(i, 1));
88
}
89
else
90
...{
91
if (Prolong)
92
...{
93
outstr.Append("...");
94
}
95
break;
96
}
97
}
98
return outstr.ToString();
99
}
100
else
101
...{
102
return instr;
103
}
104
}
105
#endregion
106
107
ÊäÈë×Ö·û´®#region ÊäÈë×Ö·û´®
108
/**//// <summary>
109
/// ·µ»Øµ¥ÐÐÊäÈëµÄ×Ö·û´®²¢½øÐÐHtml±àÂë
110
/// </summary>
111
/// <param name="instr">Òª¹ýÂ˵Ä×Ö·û´®</param>
112
/// <returns></returns>
113
public static string InString(string instr)
114
...{
115
instr = instr.Trim();
116
objregex = new Regex(" +");
117
instr = objregex.Replace(instr, " ");
118
instr = HttpContext.Current.Server.HtmlEncode(instr);
119
instr = instr.Replace("'", "''");
120
return instr;
121
}
122
123
/**//// <summary>
124
/// ·µ»Ø±íʾUrlµØÖ·µÄ×Ö·û´®²¢½øÐÐHtml±àÂë
125
/// </summary>
126
/// <param name="instr">Òª¹ýÂ˵Ä×Ö·û´®</param>
127
/// <returns></returns>
128
public static string InStrUrl(string instr)
129
...{
130
instr = instr.Trim();
131
objregex = new Regex(" +");
132
instr = objregex.Replace(instr, " ");
133
instr = instr.Replace(" ", "%20");
134
instr = instr.Replace("'", "%27");
135
instr = instr.Replace("\"", "%22");
136
instr = instr.Replace("<", "%3C");
137
instr = instr.Replace(">", "%3E");
138
instr = instr.Replace("#", "%23");
139
instr = instr.Replace("$", "%24");
140
instr = instr.Replace("\\", "%5C");
141
return instr;
142
}
143
144
/**//// <summary>
145
/// ·µ»Ø¶àÐÐÊäÈëµÄ×Ö·û´®²¢É¾³ý³¬¹ýÏÞ¶¨×ÖÊýµÄ×Ö·ûͬʱ½øÐÐHtml±àÂë
146
/// </summary>
147
/// <param name="instr">Òª¹ýÂ˵Ä×Ö·û´®</param>
148
/// <returns></returns>
149
public static string InMultiLine(string instr)
150
...{
151
return MultiLineStrConv(instr, 0, true);
152
}
153
154
/**//// <summary>
155
/// ·µ»Ø¶àÐÐÊäÈëµÄ×Ö·û´®²¢É¾³ý³¬¹ýÏÞ¶¨×ÖÊýµÄ×Ö·ûͬʱ½øÐÐHtml±àÂë
156
/// </summary>
157
/// <param name="instr">Òª¹ýÂ˵Ä×Ö·û´®</param>
158
/// <param name="NewLine">ÉèÖÃÒ»¸öÖµ£¬¸ÃÖµ±íʾÊÇ·ñÏÔʾ»»Ðзû¡£</param>
159
/// <returns></returns>
160
public static string InMultiLine(string instr, bool NewLine)
161
...{
162
return MultiLineStrConv(instr, 0, NewLine);
163
}
164
165
/**//// <summary>
166
/// ·µ»Ø¶àÐÐÊäÈëµÄ×Ö·û´®²¢É¾³ý³¬¹ýÏÞ¶¨×ÖÊýµÄ×Ö·ûͬʱ½øÐÐHtml±àÂë
167
/// </summary>
168
/// <param name="instr">Òª¹ýÂ˵Ä×Ö·û´®</param>
169
/// <param name="WordCount">±£ÁôµÄ×ÖÊý</param>
170
/// <returns></returns>
171
public static string InMultiLine(string instr, int WordCount)
172
...{
173
return MultiLineStrConv(instr, WordCount, true);
174
}
175
176
/**//// <summary>
177
/// ·µ»Ø¶àÐÐÊäÈëµÄ×Ö·û´®²¢É¾³ý³¬¹ýÏÞ¶¨×ÖÊýµÄ×Ö·ûͬʱ½øÐÐ
178
/// </summary>
179
/// <param name="instr">Òª¹ýÂ˵Ä×Ö·û´®</param>
180
/// <param name="WordCount">±£ÁôµÄ×ÖÊý</param>
181
/// <param name="NewLine">ÉèÖÃÒ»¸öÖµ£¬¸ÃÖµ±íʾÊÇ·ñÏÔʾ»»Ðзû¡£</param>
182
/// <returns></returns>
183
public static string InMultiLine(string instr, int WordCount, bool NewLine)
184
...{
185
return MultiLineStrConv(instr, WordCount, NewLine);
186
}
187
188
ÓÃÓÚ´¦Àí¶àÐÐÎı¾µÄ¹«¹²·½·¨#region ÓÃÓÚ´¦Àí¶àÐÐÎı¾µÄ¹«¹²·½·¨
189
/**//// <summary>
190
/// ·µ»Ø¶àÐÐÊäÈëµÄ×Ö·û´®²¢É¾³ý³¬¹ýÏÞ¶¨×ÖÊýµÄ×Ö·ûͬʱ½øÐÐHtml±àÂë
191
/// </summary>
192
/// <param name="instr">Òª¹ýÂ˵Ä×Ö·û´®</param>
193
/// <param name="WordCount">±£ÁôµÄ×ÖÊý</param>
194
/// <param name="NewLine">ÉèÖÃÒ»¸öÖµ£¬¸ÃÖµ±íʾÊÇ·ñÏÔʾ»»Ðзû¡£</param>
195
/// <returns></returns>
196
private static string MultiLineStrConv(string instr, int WordCount, bool NewLine)
197
...{
198
instr = instr.Trim();
199
objregex = new Regex(" +");
200
instr = objregex.Replace(instr, " ");
201
instr = instr.Replace(Environment.NewLine + " ", Environment.NewLine);
202
instr = instr.Replace(" " + Environment.NewLine, Environment.NewLine);
203
instr = instr.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
204
instr = HttpContext.Current.Server.HtmlEncode(instr);
205
instr = instr.Replace("'", "''");
206
if (NewLine)
207
...{
208
instr = instr.Replace(Environment.NewLine, "<br />" + Environment.NewLine);
209
}
210
if (WordCount > 0 && instr.Length > WordCount)
211
...{
212
instr = instr.Substring(0, WordCount);
213
}
214
return instr;
215
}
216
#endregion
217
218
/**//// <summary>
219
/// ·µ»ØMD5¼ÓÃܵÄ×Ö·û´®
220
/// </summary>
221
/// <param name="instr">Òª¼ÓÃܵÄ×Ö·û´®</param>
222
/// <returns></returns>
223
public static string EncryptPassWord(string instr)
224
...{
225
instr = instr.Trim();
226
instr = FormsAuthentication.HashPasswordForStoringInConfigFile(instr, "MD5");
227
return instr;
228
}
229
#endregion
230
231
/**//// <summary>
232
/// ¸ñʽ»¯Êä³öµÄ×Ö·û´®,³¬³öµÄ²¿·ÖʹÓÃ....ÏÔʾ
233
/// </summary>
234
/// <param name="instr">ÐèÒª¸ñʽ»¯µÄ×Ö·û´®</param>
235
/// <param name="count">½ØÈ¡¶àÉÙλÊý</param>
236
/// <returns></returns>
237
public static string Out(string instr, int count)
238
...{
239
return OutString(instr, count, true);//Òª¸ñʽ»¯µÄ×Ö·û´®¼°Òª±£ÁôµÄ×ÖÊý
240
}
241
/**//// <summary>
242
/// ¸ßÁÁÊä³ö×Ö·û´®,TrueÏÔʾºìÉ«,BlueÏÔʾÀ¼É«¡£
243
/// </summary>
244
/// <param name="instr">ÐèÒª¸ñʽ»¯µÄ×Ö·û´®</param>
245
/// <param name="light">ÊÇ·ñÐèÒª¼Ó¸ßÁÁÏÔʾ</param>
246
/// <returns></returns>
247
public static string HighLight(string instr, bool light)
248
...{
249
if (light)
250
...{
251
instr = "<span style='color:red'>" + instr + "</span>";//Òª¼ÓÁÁµÄÎı¾£¬Red
252
}
253
else
254
...{
255
instr = "<span style='color:blue'>" + instr + "</span>";//Òª¼ÓÁÁµÄÎı¾£¬Blue
256
}
257
return instr;
258
}
259
}
260