Java Utililty Methods UTF from

List of utility methods to do UTF from

Description

The list of methods to do UTF from are organized into topic(s).

Method

StringtoUtf8Path(String path)
to Utf Path
try {
    path = new String(path.getBytes("ISO-8859-1"), Charset.forName("UTF-8"));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
return path;
StringtoUTF8String(byte[] b, int offset, int length)
to UTF String
return new String(b, offset, length, StandardCharsets.UTF_8);
StringtoUtf8String(String source)
to Utf String
if (source == null) {
    return null;
if (source.equals(new String(source.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
    return new String(source.getBytes("ISO-8859-1"), "UTF-8");
return source;
StringtoUTF8String(String str)
removes all bad characters not suitable for UTF-8 encoding
if (str == null)
    return null;
return str.replaceAll("([\\ud800-\\udbff\\udc00-\\udfff])", "");
StringtoUTF_8(String s)
to UT_
if (s == null || s.length() < 1)
    return s;
byte[] temp_b = s.getBytes("ISO-8859-1");
String sTmpStr = new String(temp_b, "UTF-8");
return sTmpStr;
StringtoUTFBody(String str)
to UTF Body
int strlen = str.length();
int utflen = 0;
int c, count = 0;
for (int i = 0; i < strlen; i++) {
    c = str.charAt(i);
    if ((c >= 0x0001) && (c <= 0x007F)) {
        utflen++;
    } else if (c > 0x07FF) {
...
byte[]toUtfBytes(final String s)
Converts the supplied string into its UTF-8 representation.
try {
    byte[] b = s.getBytes("UTF-8");
    return (b);
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("This java machine is stupid. The code will die. Cake.");
StringtoUtfString(char[] encoded)
Convert a char array to UTF string
String s = new String(encoded);
return s;