2

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". It prints only "The 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.

Amit
  • 30,756
  • 6
  • 57
  • 88
  • 1
    When you say "it doesn't print all" how are you trying to print the message? Can you please provide the code you're using to print the message? – Samuel Apr 25 '17 at 04:22
  • Are you supplying this through a web page? If so, you'd have to deal with the `&` character _before_ this method, not within it. – Dawood ibn Kareem Apr 25 '17 at 04:24
  • can u edit your code with outageMsg value – manikant gautam Apr 25 '17 at 04:36
  • public void setOutageMsg(String outageMsg) { //outage msg issue if(outageMsg==null){ this.outageMsg = outageMsg; } else{ outageMsg=outageMsg.replaceAll("&","&").replaceAll("&","%26"); this.outageMsg = outageMsg; } } – Sakshi Jauhari Apr 25 '17 at 15:06
  • the above code worked correctly for '&' encoding but i need to have a code which works for all special characters " !@#$%*()............" any suggestions? – Sakshi Jauhari Apr 25 '17 at 15:07

2 Answers2

-1

Try this, It may help you

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

or

outageMsg=outageMsg.replaceAll("&(?!amp;)","&").replaceAll("&","\u0026"); 
Vinit Solanki
  • 1,863
  • 2
  • 15
  • 29
  • thanks for the suggestion. The code for '&' works. Now I need to encode for all the special characters which includes " % *#@()!" and do not want to seperately encode each and every character.Any suggestions? – Sakshi Jauhari Apr 25 '17 at 13:16
  • Answer is right so why i was down voted ? any ways your string can have all special characters or you need replace for looking for "% *#@()!" only ? – Vinit Solanki Apr 25 '17 at 14:40
  • string can have all special characters. – Sakshi Jauhari Apr 25 '17 at 15:01
  • "&(?!amp;)" is regex, you can for other special characters as well – Vinit Solanki Apr 25 '17 at 15:05
  • I dont want to add seperate line of code encoding for all characters instead 1 code which will encode all special chars when i pass a String – Sakshi Jauhari Apr 25 '17 at 15:09
  • suppose you are replacing "&(?!amp;)" with "&", and then what other special character will replace with ? Actually you need use replaceAll method for each and every special character. – Vinit Solanki Apr 25 '17 at 15:12
-1

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;
       }
Chetan Ashtivkar
  • 194
  • 2
  • 15