Java String Encode by Charset encode(String value, Charset charset)

Here you can find the source of encode(String value, Charset charset)

Description

Uses URLEncoder to translate a string into application/x-www-form-urlencoded format, using the given Charset.

License

Apache License

Declaration

public static String encode(String value, Charset charset) 

Method Source Code


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

import static java.nio.charset.StandardCharsets.*;
import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;
import java.nio.charset.Charset;

public class Main {
    /** Uses URLEncoder to translate a string into application/x-www-form-urlencoded format, using the given Charset. */
    public static String encode(String value, Charset charset) {
        try {/*from   w  w  w  . j av  a2  s  .  c  om*/
            return URLEncoder.encode(value, charset.name());
        } catch (UnsupportedEncodingException ex) {
            throw new IllegalArgumentException("Characterset is not supported.");
        }
    }

    /** Uses URLEncoder to translate a string into application/x-www-form-urlencoded format, using UTF-8 as Charset. */
    public static String encode(String value) {
        return encode(value, UTF_8);
    }
}

Related

  1. encode(Charset charset, String string)
  2. encode(final String str, final Charset charset)
  3. encode(String charsetName, char[] chars, int offset, int length)
  4. encode(String s, Charset encoding)
  5. encode(String text, Charset charset)
  6. encodeBase64(String s, Charset cs)
  7. encodeCHARSET(String string, Charset charset)
  8. encodeFormFields(final String content, final Charset charset)
  9. encodeOneChar(CharsetEncoder encoder, int uchar)