How to escape a few special characters in a string while Encoding. URL need not be encoded, only the string which has to be appended to the URL

StackOverflow https://stackoverflow.com/questions/22966962

Question

Original string

"<bodytext>@#$%&</bodytext>"

Needed after encoding

 %3CbodyText%3E%40%23%24%25%26amp%3B%3C%2FbodyText%3E

What I am getting after ASCII or UTF-8 encoding

 %3CbodyText%3E%40%23%24%25%26%3C%2FbodyText%3E

My piece of code ASCII:URLEncoder.encode(mystring, "US_ASCII").replaceAll("\\+","%20");

My piece of code ASCII:URLEncoder.encode(mystring, "utf-8").replaceAll("\\+","%20")

Concentrate on the '&' which is not being encoded as i want

I need Euro,Pound signs to be supported as well which are on Android keyboard

Was it helpful?

Solution

Try this:

public static String forHTML(String aText){
     final StringBuilder result = new StringBuilder();
     final StringCharacterIterator iterator = new StringCharacterIterator(aText);
     char character =  iterator.current();
     while (character != CharacterIterator.DONE ){
       if (character == '<') {
         result.append("&lt;");
       }
       else if (character == '>') {
         result.append("&gt;");
       }
       else if (character == '&') {
         result.append("&amp;");
      }
       else if (character == '\"') {
         result.append("&quot;");
       }
       else if (character == '\t') {
         addCharEntity(9, result);
       }
       else if (character == '!') {
         addCharEntity(33, result);
       }
       else if (character == '#') {
         addCharEntity(35, result);
       }
       else if (character == '$') {
         addCharEntity(36, result);
       }
       else if (character == '%') {
         addCharEntity(37, result);
       }
       else if (character == '\'') {
         addCharEntity(39, result);
       }
       else if (character == '(') {
         addCharEntity(40, result);
       }
       else if (character == ')') {
         addCharEntity(41, result);
       }
       else if (character == '*') {
         addCharEntity(42, result);
       }
       else if (character == '+') {
         addCharEntity(43, result);
       }
       else if (character == ',') {
         addCharEntity(44, result);
       }
       else if (character == '-') {
         addCharEntity(45, result);
       }
       else if (character == '.') {
         addCharEntity(46, result);
       }
       else if (character == '/') {
         addCharEntity(47, result);
       }
       else if (character == ':') {
         addCharEntity(58, result);
       }
       else if (character == ';') {
         addCharEntity(59, result);
       }
       else if (character == '=') {
         addCharEntity(61, result);
       }
       else if (character == '?') {
         addCharEntity(63, result);
       }
       else if (character == '@') {
         addCharEntity(64, result);
       }
       else if (character == '[') {
         addCharEntity(91, result);
       }
       else if (character == '\\') {
         addCharEntity(92, result);
       }
       else if (character == ']') {
         addCharEntity(93, result);
       }
       else if (character == '^') {
         addCharEntity(94, result);
       }
       else if (character == '_') {
         addCharEntity(95, result);
       }
       else if (character == '`') {
         addCharEntity(96, result);
       }
       else if (character == '{') {
         addCharEntity(123, result);
       }
       else if (character == '|') {
         addCharEntity(124, result);
       }
       else if (character == '}') {
         addCharEntity(125, result);
       }
       else if (character == '~') {
         addCharEntity(126, result);
       }
       else {
         //the char is not a special one
         //add it to the result as is
         result.append(character);
       }
       character = iterator.next();
     }
     return result.toString();
  }

Check this link for more information. Edit:

if (character == '£') {
    result.append("&pound;");
}
else  if (character == '€') {
        result.append("&euro;");
}

Check here for more.

OTHER TIPS

The result you got is correct. On the other hand, if you wanted some characters not actually encoded, but HTML-escaped, you need to handle them manually beforehand -- like you did for space.

So, maybe this will help:

public String myEncodeHTML(String s) {
  s = s.replaceAll(" ", "%20");
  s = s.replaceAll("&", "&amp;");
  s = s.replaceAll("€", "&euro;");
  s = s.replaceAll("£", "&pound;");
  return URLEncoder.encode(s, "US_ASCII");
}

Remember to save the source file in UTF8, otherwise the pound and euro characters will be corrupted.

This seems to me to be a very strange way to want to escape a String. That said, this will give you the end result that you're after:

public String escapeString(String toEscape) {
    String escapedString = toEscape.replaceAll("&", "&amp;");
    escapedString = toEscape.replaceAll("\\+", "%20");
    return URLEncoder.encode(escapedString, "UTF-8");
}

Although I don't really understand why you would want to escape a String in this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top