?? codefilter.java
字號:
package com.ntsky.note;
import java.io.*;
//定義一個類CodeFilter
public class CodeFilter{
public CodeFilter(){
}
//特殊字符轉為Html
public static String toHtml(String s){
if(s == null){
s = "";
return s;
}
s = Replace(s.trim(),"&","&");
s = Replace(s.trim(),"<","<");
s = Replace(s.trim(),">",">");
s = Replace(s.trim(),"\t"," ");
s = Replace(s.trim(),"\r\n","\n");
s = Replace(s.trim(),"\n","<br>");
s = Replace(s.trim()," "," ");
s = Replace(s.trim(),"'","'");
s = Replace(s.trim(),"\\","\");
return s;
}
//逆
public static String unHtml(String s){
s = Replace(s,"<br>","\n");
s = Replace(s," "," ");
return s;
}
//Replace
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;//搜索到的老字符串的位置
//source.indexOf(oldString,posStart)檢索某子串在字符串postStart以后第一次出現的位置,如果未找到就返回一個-1。
while((pos = source.indexOf(oldString,posStart)) >= 0){//得到字符串的位置(eg:如果有<br>就執行,沒有就跳出,不要處理。)
//將以posStart起始以pos-1結束之間的內容拷貝到另一個字符串中。因為posStar從0開始的。
output.append(source.substring(posStart,pos));//append方法將文本添加到當前StringBuffer對象內容的結尾。
output.append(newString);//替換成新字符串
posStart = pos + lengthOfold;//位置也變為找到了之后的位置,pos為得到第一次出現字符的位置,lengthold為字符的長度
}
if(posStart < lengthOfsource){
//source.substring(posStart)以lengthOfsource開始的字符串拷貝到列一個字符串中
output.append(source.substring(posStart));
}
//這個方法將其內容轉換成一個可以被用于輸出的字符串對象。它允許操作對應的文本用于輸出或數據存儲。
return output.toString();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -