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

Stringencode(String s)
Escapes non-ASCII characters in URL.
try {
    boolean escaped = false;
    StringBuilder out = new StringBuilder(s.length());
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(buf, "UTF-8");
    for (int i = 0; i < s.length(); i++) {
        int c = s.charAt(i);
        if (c < 128 && c != ' ') {
...
Stringencode(String s, String enc)

Encode a given string.

if (s == null) {
    throw new IllegalArgumentException("String must not be null");
if (enc == null) {
    enc = "UTF-8";
ByteArrayInputStream bIn;
try {
...
Stringencode(String s, String encoding)
encode
int length = s.length();
int start = 0;
int i = 0;
StringBuilder result = new StringBuilder(length);
while (true) {
    while ((i < length) && isSafe(s.charAt(i))) {
        i++;
    result.append(s.substring(start, i));
    if (i >= length) {
        return result.toString();
    } else if (s.charAt(i) == ' ') {
        result.append('+');
        i++;
    } else {
        start = i;
        char c;
        while ((i < length) && ((c = s.charAt(i)) != ' ') && !isSafe(c)) {
            i++;
        String unsafe = s.substring(start, i);
        byte[] bytes = unsafe.getBytes(encoding);
        for (int j = 0; j < bytes.length; j++) {
            result.append('%');
            int val = bytes[j];
            result.append(hex.charAt((val & 0xf0) >> 4));
            result.append(hex.charAt(val & 0x0f));
    start = i;
Stringencode(String source, String charsetFrom, String charsetTo)
encode
try {
    return new String(source.getBytes(charsetFrom), charsetTo);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
return source;
Stringencode(String src)
encode
String genDelims = ":/?#[]@";
String subDelims = "!$&'()*+,;=";
String unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~";
String okChars = genDelims + subDelims + unreserved + "%"; 
String filesep = System.getProperty("file.separator");
if ("\\".equals(filesep)) {
    src = src.replaceAll("\\\\", "/");
String encoded = "";
try {
    byte[] bytes = src.getBytes("UTF-8");
    for (int pos = 0; pos < bytes.length; pos++) {
        if (okChars.indexOf(bytes[pos]) >= 0) {
            encoded += (char) bytes[pos];
        } else {
            encoded += String.format("%%%02X", bytes[pos]);
} catch (UnsupportedEncodingException uee) {
return encoded;
byte[]encode(String src, String charset)
encode
if (src == null) {
    return null;
try {
    return src.getBytes(charset);
} catch (UnsupportedEncodingException e) {
    return src.getBytes();
Stringencode(String str)
encode
byte[] b = null;
String s = null;
try {
    b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
if (b != null) {
...
voidencode(String str, int start, int end, Writer writer)
encode
for (int i = start; i < end; i++) {
    char ch = str.charAt(i);
    String special = getSpecial(ch);
    if (special != null) {
        writer.write(special);
    } else {
        if ((ch & 0xff) != 0) {
            writer.write("&#"); 
...
InputStreamencode(String text, String charset)
encode
try {
    return new ByteArrayInputStream(text.getBytes(charset));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    throwCoreException(e);
return null;
Stringencode(String text, String encoding)
encode
return MimeUtility.encodeText(text, encoding, "Q");