过滤字符串中的Null和空格

StringUtil.java

public class StringUtil7 {
    private String sourceStr;//源字符串
    private String targetStr;//替换后的字符串
    public String getSourceStr() {
        return sourceStr;
    }
    public void setSourceStr(String sourceStr) {
        this.sourceStr = sourceStr;
    }
    public String getTargetStr() {
        if(sourceStr==null){
            sourceStr="";
        }
        sourceStr=sourceStr.replaceAll(" ", "");
        targetStr=sourceStr;
        return targetStr;
    }
    public void setTargetStr(String targetStr) {
        this.targetStr = targetStr;
    }
    
}

index.jsp


    
请输入字符串:

result.jsp


    <%
        request.setCharacterEncoding("utf-8");
        String sourceStr= request.getParameter("sourceStr");
    %>
    
    
    
过滤之前的字符串:
过滤之后的字符串:
21.PNG

你可能感兴趣的:(过滤字符串中的Null和空格)