Java UTF8 Encode toUTF8(String sourceStr)

Here you can find the source of toUTF8(String sourceStr)

Description

to UTF

License

Apache License

Declaration

public static String toUTF8(String sourceStr) throws UnsupportedEncodingException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;

public class Main {

    public static String toUTF8(String sourceStr) throws UnsupportedEncodingException {
        return changeEncoding(sourceStr, "ISO8859-1", "UTF-8");
    }/*from ww  w  .j  ava2  s  . c  o  m*/

    public static String changeEncoding(String sourceStr, String sourceEncoding, String targetEncoding)
            throws UnsupportedEncodingException {
        if (isEmpty(sourceStr)) {
            return sourceStr;
        }
        byte[] bytes = sourceStr.getBytes(sourceEncoding);
        return new String(bytes, targetEncoding);
    }

    public static boolean isEmpty(String sourceStr) {
        return (sourceStr == null || sourceStr.length() == 0);
    }

    public static String isEmpty(String sourceStr, String errorMsg) {
        if (isEmpty(sourceStr)) {
            return errorMsg;
        }
        return "";
    }
}

Related

  1. isUTF8(String encoding)
  2. toUTF8(byte[] bytes)
  3. toUTF8(String content)
  4. toUTF8(String isoString)
  5. toUTF8(String s)
  6. toUTF8(String str)
  7. toUTF8(String str)
  8. toUtf8(String str)
  9. toUTF8(String str)