?? strformat.java
字號:
package news;/** * Title: 字符串格式化工具 * Description: * @author: WineFox * @version 1.0 */public class strFormat { /** * 字符串替換,將 source 中的 oldString 全部換成 newString * * @param source 源字符串 * @param oldString 老的字符串 * @param newString 新的字符串 * @return 替換后的字符串 * 用于輸入的表單字符串轉化成HTML格式的文本 */ public static void main (String [] args) {} public static String Replace(String source, String oldString, String newString) { StringBuffer output = new StringBuffer(); int lengthOfSource = source.length(); // 源字符串長度 int lengthOfOld = oldString.length(); // 老字符串長度 int posStart = 0; // 開始搜索位置 int pos; // 搜索到老字符串的位置 while ((pos = source.indexOf(oldString, posStart)) >= 0) { output.append(source.substring(posStart, pos)); output.append(newString); posStart = pos + lengthOfOld; } if (posStart < lengthOfSource) { output.append(source.substring(posStart)); } return output.toString(); } /* public static String ReplaceIgnoreCase(String source, String oldString, String newString) { } */ /** * 將字符串格式化成 HTML 代碼輸出 * 只轉換特殊字符,適合于 HTML 中的表單區域 * * @param str 要格式化的字符串 * @return 格式化后的字符串 */ public static String toHtmlInput(String str) { if (str == null) return null; String html = new String(str); html = Replace(html, "&", "&"); html = Replace(html, "<", "<"); html = Replace(html, ">", ">"); return html; } /** * 將字符串格式化成 HTML 代碼輸出 * 除普通特殊字符外,還對空格、制表符和換行進行轉換, * 以將內容格式化輸出, * 適合于 HTML 中的顯示輸出 * * @param str 要格式化的字符串 * @return 格式化后的字符串 */ public static String toHtml(String str) { if (str == null) return null; String html = new String(str); html = toHtmlInput(html); html = Replace(html, "\r\n", "\n"); html = Replace(html, "\n", "<br>\n"); html = Replace(html, "\t", " "); html = Replace(html, " ", " "); return html; } /** * 將普通字符串格式化成數據庫認可的字符串格式 * * @param str 要格式化的字符串 * @return 合法的數據庫字符串 */ public static String toSql(String str) { String sql = new String(str); return Replace(sql, "'", "''"); } /* public static void main(String[] args) { String s = "<html> ddd"; Format f = new Format(); System.out.println(f.toHtmlInput(s)); System.out.println(f.toHtml(s)); } */}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -