Java Utililty Methods String Encode

List of utility methods to do String Encode

Description

The list of methods to do String Encode are organized into topic(s).

Method

StringencodeText(String str)
encode string with RFC2047
try {
    return MimeUtility.encodeText(str, "utf-8", "B");
} catch (UnsupportedEncodingException e) {
    throw new IllegalStateException(e);
StringencodeText(String str, String fromEnc, String toEnc)
Encode a string from the specific encoding name to other encoding name.
return (str == null) ? null : new String(str.getBytes(fromEnc), toEnc);
StringencodeTexts(String s)
Encode non ascii characters present in email headers
String ret = s;
if (s != null) {
    try {
        ret = MimeUtility.encodeText(s, "ISO-8859-1", null);
    } catch (UnsupportedEncodingException e) {
return ret;
...
voidencodeTextValue(char[] characters, int offset, int length, Writer writer)
encode Text Value
for (int i = offset; i < offset + length; i++) {
    char c = characters[i];
    switch (c) {
    case '<':
        writer.write(LT, 0, LT.length);
        break;
    case '>':
        writer.write(GT, 0, GT.length);
...
StringencodeThoroughly(String s)
encode Thoroughly
String result;
try {
    result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!")
            .replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")")
            .replaceAll("\\%7E", "~");
} catch (UnsupportedEncodingException e) {
    result = s;
return result;
StringencodeToFlex(Object o)
encode To Flex
String url = java.net.URLEncoder.encode((String) o, "utf-8");
url = url.replaceAll("\\+", "%20");
return url;
StringencodeToForm(Map msg)
encode To Form
StringBuffer sb = new StringBuffer();
String name;
Object value;
Iterator it = msg.keySet().iterator();
while (it.hasNext()) {
    name = (String) it.next();
    value = URLEncoder.encode((String) msg.get(name));
    sb.append(name + "=" + value);
...
StringencodeToString(String s)
encode To String
try {
    return new String(encodeToChar(s.getBytes(ENCODING), false));
} catch (UnsupportedEncodingException ignore) {
    return null;
StringencodeURNComponent(String value)
Encode data to URN fragment.
try {
    return URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("%21", "!").replace("%27", "\'")
            .replace("%28", "(").replace("%29", ")").replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
    throw new IllegalStateException(e);
StringencodeUTF8(String input, boolean keepSpaces)
Encod input as UTF-8 while converting %20 (space) to a space
if (input == null) {
    return null;
String encodedInput;
try {
    encodedInput = URLEncoder.encode(input, UTF_8);
} catch (UnsupportedEncodingException e) {
    return input;
...