codefilter.java

来自「NTsky新闻发布v1.0(提供JavaBean)」· Java 代码 · 共 56 行

JAVA
56
字号
package ntsky.common;

import java.io.*;

public class CodeFilter{
	public CodeFilter(){
	}
	public static String change(String s){
	  s = toHtml(s);
	  return s;
	}

	//特殊字符转为Html
	public static String toHtml(String s){
		if(s == null){
			s = "";
			return s;
		}
		s = Replace(s,"&","&");
	    s = Replace(s,"<","&lt;");
		s = Replace(s,">","&gt;");
		s = Replace(s,"\t","    ");
		s = Replace(s,"\r\n","\n");
		s = Replace(s,"\n","<br>");
		s = Replace(s,"  "," &nbsp;");
		s = Replace(s,"'","&#39;");
		s = Replace(s,"\\","&#92;");
		return s;
    }
	//逆
    public static String unHtml(String s){	
		s = Replace(s,"<br>","\n");
		s = Replace(s,"&nbsp;"," ");
		return s;
  	}
 
  //Replace
   public static String Replace(String source,String oldString,String newString){
		if(source == null) return null;
	    StringBuffer output = new StringBuffer();
		int lengOfsource = source.length();
		int lengOfold = 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 + lengOfold;
		}
		if(posStart < lengOfsource){
	        output.append(source.substring(posStart));
		}
		return output.toString();
   }
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?