String to ISO-8859-1 - Java Internationalization

Java examples for Internationalization:Charset

Description

String to ISO-8859-1

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "java2s.com";
        System.out.println(toISO_8859_1(str));
    }/* ww  w . ja  v a2  s  . c  o  m*/

    public static final String ISO_8859_1 = "ISO-8859-1";

    public static String toISO_8859_1(String str)
            throws UnsupportedEncodingException {
        return changeCharset(str, ISO_8859_1);
    }

    public static String changeCharset(String str, String newCharset)
            throws UnsupportedEncodingException {
        if (str != null) {
            byte[] bs = str.getBytes();
            return new String(bs, newCharset); // 
        }
        return null;
    }

    public static String changeCharset(String str, String oldCharset,
            String newCharset) throws UnsupportedEncodingException {
        if (str != null) {
            // 
            byte[] bs = str.getBytes(oldCharset);
            return new String(bs, newCharset);
        }
        return null;
    }
}

Related Tutorials