|
Posted on
2009-11-14 16:15
lann
阅读( 643)
评论()
编辑
收藏
举报
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code 1 using System; 2 using System.Text; 3 using System.Text.RegularExpressions; 4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif) 5 namespace bjmti 6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) { 7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 8 /// include 的摘要说明。 9 /// </summary> 10 public class include 11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 12 public include() 13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 14 // 15 // TODO: 在此处添加构造函数逻辑 16 // 17 } 18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.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="inputString">要截取的字符串</param> 24 /// <param name="len">要截取的长度</param> 25 /// <returns>string</returns> 26 /// 27 public static string CutString(string inputString,int len) 28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 29 30 31 ASCIIEncoding ascii = new ASCIIEncoding(); 32 int tempLen=0; 33 string tempString=""; 34 byte[] s = ascii.GetBytes(inputString); 35 for(int i=0;i<s.Length;i++) 36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 37 if((int)s[i]==63) 38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 39 tempLen+=2; 40 } 41 else 42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 43 tempLen+=1; 44 } 45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 46 try 47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 48 tempString+=inputString.Substring(i,1); 49 } 50 catch 51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 52 break; 53 } 54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 55 if(tempLen>len) 56 break; 57 } 58 // //如果截过则加上半个省略号 59 // byte[] mybyte=System.Text.Encoding.Default.GetBytes(inputString); 60 // if(mybyte.Length>len) 61 // tempString+="…"; 62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 63 return tempString; 64 } 65 #endregion 66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 67![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) 生成由日期组成的唯一的文件名#region 生成由日期组成的唯一的文件名 68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 69 /// 生成由日期组成的唯一的文件名 70 /// </summary> 71 /// <returns>string</returns> 72 /// 73 public static string makeFileName() 74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 75 string newFileName; 76 string dateName = System.DateTime.Now.ToString("yyyyMMddhhmmss"); 77 System.Random srd = new Random(); 78 int srdName = srd.Next(1000); 79 newFileName = dateName+srdName.ToString(); 80 return newFileName; 81 } 82 #endregion 83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 84![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) 过滤特殊字符#region 过滤特殊字符 85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 86 /// 过滤特殊字符 87 /// </summary> 88 /// <param name="inputStr">字符串</param> 89 /// <returns>string</returns> 90 public static string cutBadStr(string inputStr) 91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 92 inputStr = inputStr.ToLower().Replace(",",""); 93 inputStr = inputStr.ToLower().Replace("<","<"); 94 inputStr = inputStr.ToLower().Replace(">",">"); 95 inputStr = inputStr.ToLower().Replace("%",""); 96 inputStr = inputStr.ToLower().Replace(".",""); 97 inputStr = inputStr.ToLower().Replace(":",""); 98 inputStr = inputStr.ToLower().Replace("#",""); 99 inputStr = inputStr.ToLower().Replace("&",""); 100 inputStr = inputStr.ToLower().Replace("$",""); 101 inputStr = inputStr.ToLower().Replace("^",""); 102 inputStr = inputStr.ToLower().Replace("*",""); 103 inputStr = inputStr.ToLower().Replace("`",""); 104 inputStr = inputStr.ToLower().Replace(" ",""); 105 inputStr = inputStr.ToLower().Replace("~",""); 106 inputStr = inputStr.ToLower().Replace("or",""); 107 inputStr = inputStr.ToLower().Replace("and",""); 108 return inputStr; 109 110 } 111 #endregion 112![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 113![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) 过滤html标记#region 过滤html标记 114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 115 /// 过滤html标记 116 /// </summary> 117 /// <param name="HTMLStr">要过滤的字符串</param> 118 /// <returns>string</returns> 119 /// 120![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 121 public static string CutHTML(string strHtml) 122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) string [] aryReg = { 124 @"<script[^>]*?>.*?</script>", 125 126 @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", 127 @"([\r\n])[\s]+", 128 @"&(quot|#34);", 129 @"&(amp|#38);", 130 @"&(lt|#60);", 131 @"&(gt|#62);", 132 @"&(nbsp|#160);", 133 @"&(iexcl|#161);", 134 @"&(cent|#162);", 135 @"&(pound|#163);", 136 @"&(copy|#169);", 137 @"&#(\d+);", 138 @"-->", 139 @"<!--.*\n" 140 }; 141 142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) string [] aryRep = { 143 "", 144 "", 145 "", 146 "\"", 147 "&", 148 "<", 149 ">", 150 " ", 151 "\xa1",//chr(161), 152 "\xa2",//chr(162), 153 "\xa3",//chr(163), 154 "\xa9",//chr(169), 155 "", 156 "\r\n", 157 "" 158 }; 159 160 string newReg =aryReg[0]; 161 string strOutput=strHtml; 162 for(int i = 0;i<aryReg.Length;i++) 163![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 164 Regex regex = new Regex(aryReg[i],RegexOptions.IgnoreCase); 165 strOutput = regex.Replace(strOutput,aryRep[i]); 166 } 167 strOutput.Replace("<",""); 168 strOutput.Replace(">",""); 169 strOutput.Replace("\r\n",""); 170 return strOutput; 171 } 172 #endregion 173![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 174![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) 标题固定长度#region 标题固定长度 175 176 177![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 178 /// <table style="font-size:12px"> 179 /// <tr><td><b>功能描述</b>:填充或截断原始字符串为指定长度 </td></tr> 180 /// <tr><td><b>创 建 人</b>: </td></tr> 181 /// <tr><td><b>创建时间</b>:</td></tr> 182 /// </table> 183 /// </summary> 184 /// <param name="strOriginal">原始字符串</param> 185 /// <param name="maxTrueLength">字符串的字节长度</param> 186 /// <param name="chrPad">填充字符</param> 187 /// <param name="blnCutTail">字符串的字节长度超过maxTrueLength时截断多余字符</param> 188 /// <returns>填充或截断后的字符串</returns> 189 static public string PadRightTrueLen(string strOriginal,int maxTrueLength,char chrPad,bool blnCutTail) 190![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 191 string strNew = strOriginal; 192 193 if (strOriginal==null || maxTrueLength<=0) 194![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 195 strNew = ""; 196 return strNew; 197 } 198![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 199 int trueLen = TrueLength(strOriginal); 200 if (trueLen>maxTrueLength)//超过maxTrueLength 201![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 202 if (blnCutTail)//截断 203![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 204 for(int i=strOriginal.Length-1; i>0; i--) 205![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 206 strNew = strNew.Substring(0,i); 207 if (TrueLength(strNew) == maxTrueLength) 208 break; 209 else if (TrueLength(strNew) < maxTrueLength) 210![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 211 strNew += chrPad.ToString(); 212 break; 213 } 214 } 215 } 216 } 217 else//填充 218![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 219 for(int i=0;i<maxTrueLength-trueLen;i++) 220![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 221 strNew += chrPad.ToString(); 222 } 223 } 224![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 225 return strNew; 226 } 227![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 228 //主方法 229 public static string CutStringTitle(string inputString,int i) 230![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 231 return PadRightTrueLen(inputString,i,' ',true); 232 } 233 234![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 235 /// <table style="font-size:12px"> 236 /// <tr><td><b>功能描述</b>:字符串的字节长度 </td></tr> 237 /// <tr><td><b>创 建 人</b>: </td></tr> 238 /// <tr><td><b>创建时间</b>:</td></tr> 239 /// </table> 240 /// </summary> 241 /// <param name="str">字符串</param> 242 /// <returns>字符串的字节长度</returns> 243 static public int TrueLength(string str) 244![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 245 int lenTotal = 0; 246 int n = str.Length; 247 string strWord = ""; 248 int asc; 249 for(int i=0;i<n;i++) 250![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 251 strWord = str.Substring(i,1); 252 asc = Convert.ToChar(strWord); 253 if ( asc < 0 || asc > 127 ) 254 lenTotal = lenTotal + 2; 255 else 256 lenTotal = lenTotal + 1; 257 } 258![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 259 return lenTotal; 260 } 261 #endregion 262![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 263![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) 关闭当前窗口(静态)#region 关闭当前窗口(静态) 264![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 265 /// (Descript) 关闭当前窗口(静态) 266 /// (Author) warfu 267 /// (Date) 2007-08-28 268 /// </summary> 269 public static void CloseWindow() 270![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 271 System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=JAVASCRIPT>window.close()</SCRIPT>"); 272 } 273 #endregion 274![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 275![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) 打开没有Toolbar的新窗口(静态)#region 打开没有Toolbar的新窗口(静态) 276![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 277![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 278 /// 返回js脚本——window.open('','') 279 /// </summary> 280 /// <param name="url">网址</param> 281 /// <param name="name">目标</param> 282 /// <returns></returns> 283 public static string openWeb(string url,string name) 284![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 285 //输出提示信息 286 return "<script language=javascript>window.open('"+ url +"','"+ name +"')</script>"; 287 } 288![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 289![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 290 /// 打开没有Toolbar的新窗口(静态) 291 /// </summary> 292 /// <param name="str_Url">页面路径</param> 293 /// <param name="int_Height">窗口高度</param> 294 /// <param name="int_Width">窗口宽度</param> 295 /// <param name="str_WebName">窗口名称</param> 296 public static void OpenWindow(string str_Url,int int_Height,int int_Width,string str_WebName) 297![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 298 //构造JAVASCRIPT 299 string str_JavaScript = @"<Script language='JavaScript'> 300 window.open('"+str_Url+@"','"+str_WebName+@"','height="+ int_Height +@"','width="+ int_Width +@"','top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no,directories=no'); 301 </Script>"; 302![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 303 System.Web.HttpContext.Current.Response.Write(str_JavaScript); 304![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 305 306 } 307 #endregion 308![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 309 } 310 }
|