?? dataformat.java
字號:
package bookshop.util;/** * <p>負責(zé)字符串的處理 </p> */import java.lang.*;import java.util.*;public class dataFormat { public dataFormat() { } /** * 把字符串轉(zhuǎn)換成適合于網(wǎng)頁顯示的文本 * @param s * @return */ public static String toHtml(String s) { if (s==null) return s; s=strReplace(s,"&","&"); s=strReplace(s,"<","<"); s=strReplace(s,">",">"); s=strReplace(s,"\"","""); s=parseReturn(s,"<br>\n "); return s; } /** * 把字符串sBody中的sFrom用sTo替換 * @param sBody * @param sFrom * @param sTo * @return */ public static String strReplace(String sBody, String sFrom, String sTo) { int i,j,k,l; if (sBody==null || sBody.equals("")) return ""; i = 0; j = sFrom.length(); k = sTo.length(); StringBuffer sss = new StringBuffer(sBody.length()); boolean bFirst=true; l = i; while (sBody.indexOf(sFrom,i)!=-1) { i = sBody.indexOf(sFrom,i); sss.append(sBody.substring(l,i)); sss.append(sTo); i += j; l = i; } sss.append(sBody.substring(l)); return sss.toString(); } /** * 把字符串中的"\r\n"轉(zhuǎn)換成"\n" * @param String sBody : 要進行替換操作的字符串 * @param String sEndwith : 要替換成為的字符串 */ public static String parseReturn(String sBody, String sEndwith) { StringTokenizer t = new StringTokenizer(sBody, "\r\n"); StringBuffer sss = new StringBuffer(sBody.length()); boolean bFirst=true; if (sEndwith.trim().equals("")) sEndwith="\n"; while (t.hasMoreTokens()) { String s=t.nextToken(); s=s.trim(); while (s.startsWith(" ")) s=s.substring(2); if (!s.equals("")) { if (bFirst) { bFirst=false; } else { sss.append(sEndwith); } sss.append(s); } } return sss.toString(); } /** * 將字符串格式化成 HTML 代碼輸出 * 只轉(zhuǎn)換特殊字符,適合于 HTML 中的表單區(qū)域 * @param str 要格式化的字符串 * @return 格式化后的字符串 */ public static String toHtmlInput(String str) { if (str == null) return null; String html = new String(str); html = strReplace(html, "&", "&"); html = strReplace(html, "<", "<"); html = strReplace(html, ">", ">"); return html; } /** * 將普通字符串格式化成數(shù)據(jù)庫認可的字符串格式 * * @param str 要格式化的字符串 * @return 合法的數(shù)據(jù)庫字符串 */ public static String toSql(String str) { String sql = new String(str); return strReplace(sql, "'", "''"); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -