简体   繁体   English

用Java编码特殊字符

[英]encoding special characters in java

I have a string message to display which contains special characters but it doesnt print all. 我有一个字符串消息要显示,其中包含特殊字符,但不能完全打印出来。 For example if I give message like "The P & A company does the work". 例如,如果我给出“ P&A公司完成工作”之类的消息。 It prints only "The P". 它仅打印“ P”。

public void setOutageMsg(String outageMsg) {
        //outage msg issue
        if(outageMsg==null){
            this.outageMsg = outageMsg;
        }
        else{
            outageMsg=outageMsg.replaceAll("&","&").replaceAll("&","%26");  
            this.outageMsg = outageMsg;
        }
    }

Similarly, I need to have a single code for all the special characters. 同样,我需要为所有特殊字符使用一个代码。

Try this, It may help you 试试这个,可能对您有帮助

outageMsg=outageMsg.replaceAll("&","&").replaceAll("&","\u0026");

or 要么

outageMsg=outageMsg.replaceAll("&(?!amp;)","&").replaceAll("&","\u0026"); 

You can write something along these lines 您可以按照以下方式写点东西

    public void setOutageMsg(String outageMsg) {
            //outage msg issue
            if(outageMsg==null){
                this.outageMsg = outageMsg;
            }
            else{
                this.outageMsg = getFormattedString(outageMsg);
            }
        }

    private String getFormattedString(String outageMsg){
           outageMsg.replaceAll("&","&");
           outageMsg.replaceAll("a","b");
           //...
           return outageMsg;
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM