Java String Encode encode(String encoding, String text)

Here you can find the source of encode(String encoding, String text)

Description

Encodes a String from unicode to the specified encoding.

License

Open Source License

Parameter

Parameter Description
encoding a parameter
text a parameter

Return

byte[]

Declaration

public static byte[] encode(String encoding, String text) 

Method Source Code


//package com.java2s;
import java.io.UnsupportedEncodingException;

public class Main {
    /**/*www .  j  a v  a2  s  . c o m*/
     * Encodes a String from unicode to the specified encoding. This method
     * returns null if the encoding is not supported
     * 
     * @param encoding
     * @param text
     * @return byte[]
     */
    public static byte[] encode(String encoding, String text) {
        if (text != null) {
            if (encoding == null)
                return text.getBytes();

            try {
                return text.getBytes(encoding);
            } catch (UnsupportedEncodingException e) {
            }
        }

        return null;
    }
}

Related

  1. encode(final String s)
  2. encode(final String s, final String encoding)
  3. encode(final String source, final String encoding, BitSet notEncoded)
  4. encode(String data, String encode)
  5. encode(String encoding, String string)
  6. encode(String name)
  7. encode(String s)
  8. encode(String s)
  9. encode(String s)