filterstring.java

来自「具有代表性的示例,是初学java编程者不错的选择.」· Java 代码 · 共 49 行

JAVA
49
字号
/*
 * Created on 2005-8-14
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.chapter2;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FilterString {
    public static String filter(String value) {
        if (value == null)
            return (null);

        StringBuffer result = new StringBuffer();
        for (int i = 0; i < value.length(); i++) {
            char ch = value.charAt(i);
            if (ch == '<')
                result.append("&lt;");
            else if (ch == '>')
                result.append("&gt;");
            else if (ch == '&')
                result.append("&amp;");
            else if (ch == '"')
                result.append("&quot;");
            else if (ch == '\r')
                result.append("<BR>");
            else if (ch == '\n') {
                if (i > 0 && value.charAt(i - 1) == '\r') {
                } else {
                    result.append("<BR>");
                }
            } else if (ch == '\t')
                result.append("&nbsp;&nbsp;&nbsp;&nbsp");
            else if (ch == ' ')
                result.append("&nbsp;");
            else
                result.append(ch);
        }
        return (result.toString());
    }

}

⌨️ 快捷键说明

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