Java String Encode encodeText(String str, String fromEnc, String toEnc)

Here you can find the source of encodeText(String str, String fromEnc, String toEnc)

Description

Encode a string from the specific encoding name to other encoding name.

License

Open Source License

Parameter

Parameter Description
str a source string
fromEnc source string encoding name
toEnc target encoding name

Return

encoded string.

Declaration

public static String encodeText(String str, String fromEnc, String toEnc) throws UnsupportedEncodingException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

public class Main {
    /**//from   w w  w. j a  v  a2s  . c  o  m
     * Encode a string from the specific encoding name to other encoding name.
     * 
     * @param str
     *            a source string
     * @param fromEnc
     *            source string encoding name
     * @param toEnc
     *            target encoding name
     * @return encoded string.
     * @exception UnsupportedEncodingException
     *                If the named encoding is not supported.
     */
    public static String encodeText(String str, String fromEnc, String toEnc) throws UnsupportedEncodingException {
        return (str == null) ? null : new String(str.getBytes(fromEnc), toEnc);
    }

    /**
     * Encode a string from "8859_1" to "default.encoding".
     * 
     * @param str
     *            a source string
     * @return encoded string.
     * @exception UnsupportedEncodingException
     *                If "default.encoding" is not supported.
     */
    public static String encodeText(String str) throws UnsupportedEncodingException {
        if (getEncoding() == null)
            return str;
        return encodeText(str, "8859_1", getEncoding());
    }

    /**
     * Default encoding name.
     * 
     * @return Quics properties <code>quics.encoding</code> property value. If
     *         the <code>quics.encoding</code> property value is null, return
     *         null.
     */
    public static String getEncoding() {
        return "euc-kr";
    }
}

Related

  1. encodeString(String strData)
  2. encodeString(String text, String charsetName)
  3. encodeString(String value)
  4. encodeStringByUTF8(String str)
  5. encodeText(String str)
  6. encodeTexts(String s)
  7. encodeTextValue(char[] characters, int offset, int length, Writer writer)
  8. encodeThoroughly(String s)
  9. encodeToFlex(Object o)