一个字符串工具类

java 代码
  1. import javax.servlet.ServletContext;   
  2. import javax.servlet.http.*;   
  3. import java.io.*;   
  4.   
  5.   
  6. public class StringUtil {   
  7.   public StringUtil() {   
  8. }    
  9. // 声明一些方便内部方法   
  10.   
  11. // ------------------------------------ 字符串处理方法 ----------------------------------------------   
  12. // 将字符串 source 中的 oldStr 替换为 newStr, 并以大小写敏感方式进行查找   
  13. public String replace(String source, String oldStr, String newStr) {   
  14. return replace(source, oldStr, newStr, true);   
  15. }   
  16.   
  17. // 将字符串 source 中的 oldStr 替换为 newStr, matchCase 为是否设置大小写敏感查找   
  18. public String replace(String source, String oldStr, String newStr,   
  19. boolean matchCase) {   
  20. if (source == null)   
  21. return null;   
  22. // 首先检查旧字符串是否存在, 不存在就不进行替换   
  23. if (source.toLowerCase().indexOf(oldStr.toLowerCase()) == -1) {   
  24. return source;   
  25. }   
  26.   
  27. int findStartPos = 0;   
  28. int a = 0;   
  29. while (a > -1) {   
  30. int b = 0;   
  31. String str1, str2, str3, str4, strA, strB;   
  32. str1 = source;   
  33. str2 = str1.toLowerCase();   
  34. str3 = oldStr;   
  35. str4 = str3.toLowerCase();   
  36. if (matchCase) {   
  37. strA = str1;   
  38. strB = str3;   
  39. }   
  40. else {   
  41. strA = str2;   
  42. strB = str4;   
  43. }   
  44. a = strA.indexOf(strB, findStartPos);   
  45. if (a > -1) {   
  46. b = oldStr.length();   
  47. findStartPos = a + b;   
  48. StringBuffer bbuf = new StringBuffer(source);   
  49. source = bbuf.replace(a, a + b, newStr) + "";   
  50. // 新的查找开始点位于替换后的字符串的结尾   
  51. findStartPos = findStartPos + newStr.length() - b;   
  52. }   
  53. }   
  54. return source;   
  55. }   
  56.   
  57. // 滤除帖子中的危险 HTML 代码, 主要是脚本代码, 滚动字幕代码以及脚本事件处理代码   
  58. public String replaceHtmlCode(String content) {   
  59. if (isEmpty(content))   
  60. return "";   
  61. // 需要滤除的脚本事件关键字   
  62. String[] eventKeywords = {   
  63. "onmouseover",   
  64. "onmouseout",   
  65. "onmousedown",   
  66. "onmouseup",   
  67. "onmousemove",   
  68. "onclick",   
  69. "ondblclick",   
  70. "onkeypress",   
  71. "onkeydown",   
  72. "onkeyup",   
  73. "ondragstart",   
  74. "onerrorupdate",   
  75. "onhelp",   
  76. "onreadystatechange",   
  77. "onrowenter",   
  78. "onrowexit",   
  79. "onselectstart",   
  80. "onload",   
  81. "onunload",   
  82. "onbeforeunload",   
  83. "onblur",   
  84. "onerror",   
  85. "onfocus",   
  86. "onresize",   
  87. "onscroll",   
  88. "oncontextmenu"  
  89. };   
  90.   
  91. content = replace(content, "<script""&ltscript"false);   
  92. content = replace(content, "</script""&lt/script"false);   
  93. content = replace(content, "<marquee""&ltmarquee"false);   
  94. content = replace(content, "</marquee""&lt/marquee"false);   
  95. content = replace(content, "/r/n""<BR>");   
  96. // 滤除脚本事件代码   
  97. for (int i = 0; i < eventKeywords.length; i++) {   
  98. content = replace(content, eventKeywords[i], "_" + eventKeywords[i], false); // 添加一个"_", 使事件代码无效   
  99. }   
  100.   
  101. return content;   
  102. }   
  103.   
  104. //******************************** 滤除 HTML 代码 为文本代码 *****************************   
  105. public String replaceHtmlToText(String input) {   
  106. if(isEmpty(input)) {   
  107. return "";   
  108. }   
  109. return setBr(setTag(input));   
  110. }   
  111.   
  112. // 滤除 HTML 标记   
  113. public String setTag(String s) {   
  114. int j = s.length();   
  115.   
  116. StringBuffer stringbuffer = new StringBuffer(j + 500);   
  117.   
  118. for(int i = 0; i < j ;i++)   
  119. if(s.charAt(i) == '<')   
  120. stringbuffer.append("&lt");   
  121. else if(s.charAt(i) == '>')   
  122. stringbuffer.append("&gt");   
  123. else if(s.charAt(i) == '&')   
  124. stringbuffer.append("&amp");   
  125. else  
  126. stringbuffer.append(s.charAt(i));   
  127.   
  128. return stringbuffer.toString();   
  129. }   
  130.   
  131. // 滤除 BR 代码   
  132.   
  133. public String setBr(String s) {   
  134. int j = s.length();   
  135.   
  136. StringBuffer stringbuffer = new StringBuffer(j + 500);   
  137.   
  138. for(int i = 0; i < j ;i++)   
  139. if(s.charAt(i) == '/n')   
  140. stringbuffer.append("");   
  141. else if(s.charAt(i) == '/r')   
  142. stringbuffer.append("");   
  143. else  
  144. stringbuffer.append(s.charAt(i));   
  145.   
  146. return stringbuffer.toString();   
  147. }   
  148.   
  149. // 滤除空格   
  150. public String setNbsp(String s) {   
  151. int j = s.length();   
  152.   
  153. StringBuffer stringbuffer = new StringBuffer(j + 500);   
  154.   
  155. for(int i = 0; i < j ;i++)   
  156. if(s.charAt(i) == ' ')   
  157. stringbuffer.append("&nbsp;");   
  158. else  
  159. stringbuffer.append(s.charAt(i));   
  160.   
  161. return stringbuffer.toString();   
  162. }   
  163.   
  164. // 判断字符串是否全是数字字符   
  165. public boolean isNumeric(String input) {   
  166. if (isEmpty(input))   
  167. return false;   
  168. for (int i = 0; i < input.length(); i++) {   
  169. char charAt = input.charAt(i);   
  170. if (charAt < '0' || charAt > '9') {   
  171. return false;   
  172. }   
  173. }   
  174. return true;   
  175. }   
  176.   
  177. // 转换由表单读取的数据的内码   
  178. public String toChi(String input) {   
  179. try {   
  180. byte[] bytes = input.getBytes("ISO8859-1");   
  181. return new String(bytes);   
  182. }   
  183. catch (Exception ex) {   
  184. }   
  185. return null;   
  186. }   
  187.   
  188. // 将单个的 ' 换成 ''; SQL 规则:如果单引号中的字符串包含一个嵌入的引号,可以使用两个单引号表示嵌入的单引号。   
  189. public String replaceSql(String input) {   
  190. return replace(input, "'""''");   
  191. }   
  192.   
  193. // 对给定字符进行 URL 编码   
  194. public String encode(String value) {   
  195. if (isEmpty(value))   
  196. return "";   
  197. return java.net.URLEncoder.encode(value);   
  198. }   
  199.   
  200. // 对给定字符进行 URL 解码   
  201. public String decode(String value) {   
  202. if (isEmpty(value))   
  203. return "";   
  204. return java.net.URLDecoder.decode(value);   
  205. }   
  206.   
  207. /**   
  208. * 判断字符串是否未空   
  209. */   
  210. public boolean isEmpty(String input) {   
  211. if (input == null || input.length() <= 0)   
  212. return true;   
  213. return false;   
  214. }   
  215.   
  216. // 将心情符号修改为对应的图片 ------------- 请修改页面中相关代码!   
  217. public String smilies(String temp) {   
  218. if (isEmpty(temp))   
  219. return "";   
  220. // 判断是否有禁止表情字符的表单值   
  221. //if( isEmpty(request("smilies")) ) {   
  222. temp = replace(temp, "/:)",   
  223. "<IMG border=0 SRC=images/brow/regular_smile.gif>");   
  224. temp = replace(temp, "/:d",   
  225. "<IMG border=0 SRC=images/brow/teeth_smile.gif>");   
  226. temp = replace(temp, "/:o""<IMG border=0 SRC=images/brow/omg_smile.gif>");   
  227. temp = replace(temp, "/:p",   
  228. "<IMG border=0 SRC=images/brow/tounge_smile.gif>");   
  229. temp = replace(temp, "/;)""<IMG border=0 SRC=images/brow/wink_smile.gif>");   
  230. temp = replace(temp, "/:(""<IMG border=0 SRC=images/brow/sad_smile.gif>");   
  231. temp = replace(temp, "/:s",   
  232. "<IMG border=0 SRC=images/brow/confused_smile.gif>");   
  233. temp = replace(temp, "/:|",   
  234. "<IMG border=0 SRC=images/brow/whatchutalkingabout_smile.gif>");   
  235. temp = replace(temp, "/:$",   
  236. "<IMG border=0 SRC=images/brow/embaressed_smile.gif>");   
  237. //}   
  238. return temp;   
  239. }   
  240.   
  241. /**  
  242. * 得到文件的扩展名.  
  243. * @param fileName 需要处理的文件的名字.  
  244. * @return the extension portion of the file's name.  
  245. */  
  246. public String getExtension(String fileName) {   
  247. if (fileName != null) {   
  248. int i = fileName.lastIndexOf('.');   
  249. if (i > 0 && i < fileName.length() - 1) {   
  250. return fileName.substring(i + 1).toLowerCase();   
  251. }   
  252. }   
  253. return "";   
  254. }   
  255.   
  256. /**  
  257. * 得到文件的前缀名.  
  258. * @param fileName 需要处理的文件的名字.  
  259. * @return the prefix portion of the file's name.  
  260. */  
  261. public String getPrefix(String fileName) {   
  262. if (fileName != null) {   
  263. int i = fileName.lastIndexOf('.');   
  264. if (i > 0 && i < fileName.length() - 1) {   
  265. return fileName.substring(0, i);   
  266. }   
  267. }   
  268. return "";   
  269. }   
  270.   
  271. // ------------------------------------ JSP 参数处理方法 ----------------------------------------------   
  272. // 一个与 ASP 类似的方法, 返回表单域的值, 并转换内码 -- 仅适用于 Tomcat 4.0   
  273. public String request(HttpServletRequest request, String fieldName) {   
  274. // POST 方法的参数没有编码错误   
  275. if (request.getMethod().equalsIgnoreCase("POST")) {   
  276. // 文件上传模式   
  277. //if(isUploadMode) {   
  278. // return request.getParameter(fieldName);   
  279. //}   
  280. // For Tomcat 4.0.6   
  281. return toChi(request.getParameter(fieldName));   
  282. }   
  283. else {   
  284. // 将通过 GET 方式发送的中文字符解码(但是必须使用 java.net.URLEncoder 进行中文字符参数的编码)   
  285. // 解码时需使用内码转换, 也可使用反编码, 即: return decode(request.getParameter(fieldName));   
  286. // 问题: decode() 仅适用于 JDK 1.3 + Tomcat 4.0   
  287. return toChi(request.getParameter(fieldName));   
  288. }   
  289. }   
  290.   
  291. // 如果表单的值是 null, 则返回 "", 避免 NullPointerException   
  292. public String request1(HttpServletRequest request, String fieldName) {   
  293. String s = request(request, fieldName);   
  294. if (s == null)   
  295. return "";   
  296. else  
  297. return s;   
  298. }   
  299.   
  300. // 获得指定表单域的值, 并将单个的 ' 换成 ''; SQL 规则:如果单引号中的字符串包含一个嵌入的引号,   
  301. // 可以使用两个单引号表示嵌入的单引号。   
  302. public String requestSql(HttpServletRequest request, String fieldName) {   
  303. return replaceSql(request1(request, fieldName));   
  304. }   
  305.   
  306. // 根据 Cookie 名称得到请求中的 Cookie 值, 需要事先给 _request 一个初始值; 如果 Cookie 值是 null, 则返回 ""   
  307. public String getCookieValue(HttpServletRequest request, String name) {   
  308. Cookie[] cookies = request.getCookies();   
  309. if (cookies == null)   
  310. return "";   
  311. for (int i = 0; i < cookies.length; i++) {   
  312. Cookie cookie = cookies[i];   
  313. if (cookie.getName().equals(name)) {   
  314. // 需要对 Cookie 中的汉字进行 URL 反编码, 适用版本: Tomcat 4.0   
  315. return decode(cookie.getValue());   
  316. // 不需要反编码, 适用版本: JSWDK 1.0.1   
  317. //return cookie.getValue();   
  318. }   
  319. }   
  320. // A cookie may not return a null value, may return a ""   
  321. return "";   
  322. }   
  323.   
  324. // 返回指定表单名的数组   
  325. public String[] getParameterValues(HttpServletRequest request, String name) {   
  326. // POST 方法的参数没有编码错误   
  327. if (request.getMethod().equalsIgnoreCase("POST")) {   
  328. // 文件上传模式   
  329. //if(isUploadMode) {   
  330. // return request.getParameterValues(name);   
  331. //}   
  332. // -- For Tomcat 4.0   
  333. return request.getParameterValues(name);   
  334. // -- For JSWDK 1.0.1   
  335. /*String values[] = _request.getParameterValues(name);  
  336. if(values != null) {  
  337. for(int i = 0; i < values.length; i++) {  
  338. values[i] = toChi(values[i]);  
  339. }  
  340. }  
  341. return values;*/  
  342. }   
  343. else {   
  344. // 将通过 GET 方式发送的中文字符解码(但是必须使用 java.net.URLEncoder 进行中文字符参数的编码)   
  345. // 解码时需使用内码转换, 也可使用反编码, 即: return decode(_request.getParameter(name));   
  346. // 问题: decode() 仅适用于 JDK 1.3 + Tomcat 4.0   
  347. String values[] = request.getParameterValues(name);   
  348. if (values != null) {   
  349. for (int i = 0; i < values.length; i++) {   
  350. values[i] = toChi(values[i]);   
  351. }   
  352. }   
  353. return values;   
  354. }   
  355. }   
  356.   
  357. // 删除指定的 Web 应用程序目录下所上传的文件   
  358. public void deleteFile(ServletContext application, String filePath) {   
  359. if (!isEmpty(filePath)) {   
  360. String physicalFilePath = application.getRealPath(filePath);   
  361. if (!isEmpty(physicalFilePath)) {   
  362. java.io.File file = new java.io.File(physicalFilePath);   
  363. file.delete();   
  364. }   
  365. }   
  366. }   
  367.   
  368. }   
  369.   
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值