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 text, String encoding)
encode
if (true)
    return text;
try {
    byte[] bs = text.getBytes(System.getProperty("file.encoding"));
    ByteArrayInputStream is = new ByteArrayInputStream(bs);
    InputStreamReader r = new InputStreamReader(is, encoding);
    char[] cs = new char[bs.length];
    int l = r.read(cs, 0, cs.length);
...
Stringencode(String val, String encoding)
encode
if (val == null) {
    return null;
return new String(val.getBytes(encoding));
Stringencode(String value, String encoding)
Encodes a String using the set of characters allowed in a URI.
int len = value.length();
StringBuilder out = new StringBuilder(len * 3 / 2);
for (int charIndex = 0; charIndex < len; charIndex++) {
    char aChar = value.charAt(charIndex);
    if ((aChar <= 127) && sValidChar[aChar]) {
        out.append(aChar);
    } else if (aChar == ' ') {
        out.append('+');
...
StringencodeBase64(String str)
encode Base
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = MimeUtility.encode(baos, "base64");
b64os.write(str.getBytes());
b64os.close();
return baos.toString().trim();
StringencodeConvenience(String str)
Performs safe URI-escaping of the '%' and ' ' characters in the string for convenience sake.
Matcher m = CONVENIENCE_ENCODABLE.matcher(str);
boolean found = m.find();
if (found) {
    final char[] hexTable = HEX_TABLE;
    StringBuilder sb = new StringBuilder(str.length() + ENCODE_ALLOWANCE);
    int findStart = 0;
    do {
        final int pos = m.start();
...
voidencodeDataPair(final StringBuilder buffer, final String key, final String value)

Encode a key/value data pair to be used in a HTTP post request.

buffer.append('&').append(encode(key)).append('=').append(encode(value));
StringencodeDoublePercent(String input)
encode Double Percent
if (input.contains("%")) {
    input = input.replaceAll("%", "%25");
return encode(input);
StringencodeDownloadFileName(String s)
encode Download File Name
try {
    return URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20");
} catch (Exception e) {
return "";
StringencodeFilename(final String filename, final String userAgent)
Hack to get the correct format of the file name, based on USER-AGENT string.
String codedFilename = null;
try {
    if (null != userAgent && -1 != userAgent.indexOf("MSIE")) {
        codedFilename = URLEncoder.encode(filename, DEFAULT_CHARSET);
    } else if (null != userAgent && -1 != userAgent.indexOf("Mozilla")) {
        codedFilename = MimeUtility.encodeText(filename, DEFAULT_CHARSET, "B");
} catch (UnsupportedEncodingException uee) {
...
StringencodeFilename(String filename, String encoding)
encode Filename
String splChars = ".*[#$%?@].*";
if (filename.matches(splChars))
    return URLEncoder.encode(filename, encoding);
return filename;