Java UTF8 Encode utf8Encode(String str)

Here you can find the source of utf8Encode(String str)

Description

utf Encode

License

Apache License

Declaration

public static String utf8Encode(String str) 

Method Source Code

//package com.java2s;
/**/*from w  w w.  j a  va 2s  .c om*/
 *  Copyright (c) 2014 http://www.lushapp.wang
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Main {

    public static String utf8Encode(String str) {
        if (!isEmpty(str) && str.getBytes().length != str.length()) {
            try {
                return URLEncoder.encode(str, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        }
        return str;
    }

    public static String utf8Encode(String str, String defultReturn) {
        if (!isEmpty(str) && str.getBytes().length != str.length()) {
            try {
                return URLEncoder.encode(str, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                return defultReturn;
            }
        }
        return str;
    }

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

Related

  1. Utf8codeCheck(String text)
  2. utf8Encode(final Collection col)
  3. utf8Encode(final String s)
  4. utf8Encode(final String value)
  5. utf8Encode(String s)
  6. utf8Encode(String str)
  7. Utf8Encode(String string)
  8. utf8Encode(String url)
  9. utf8FromString(String sIn)