Java URL Encode encode(String s)

Here you can find the source of encode(String s)

Description

Equivalent to #encode(String,String) with second parameter set to the "UTF-8" encoding.

License

Open Source License

Parameter

Parameter Description
s The string to encode.

Return

The encoded string.

Declaration

public static String encode(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w  ww .ja  va 2 s  .  com
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20081119   255374 mahutch@ca.ibm.com - Mark Hutchinson
 *******************************************************************************/

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {
    /**
     * UTF-8
     */
    public static final String UTF8 = "UTF-8";

    /**
     * Equivalent to {@link #encode(String,String)}
     * with second parameter set to the "UTF-8" encoding.
     * @param s The string to encode.
     * @return The encoded string.
     */
    public static String encode(String s) {
        return encode(s, UTF8);
    }

    /**
     * Equivalent to {@link URLEncoder#encode(String,String)},
     * only throws an unchecked {@link RuntimeException} wrapped
     * around an {@link UnsupportedEncodingException} instead of
     * an {@link UnsupportedEncodingException}.
     * @param s The string to encode.
     * @param enc The encoding to use.
     * @return The encoded string.
     */
    public static String encode(String s, String enc) {
        try {
            return URLEncoder.encode(s, enc);
        } catch (UnsupportedEncodingException e) {
            // TODO: MSG_BROKEN_VM_DOES_NOT_SUPPORT_UTF-8
            throw new RuntimeException("%MSG_BROKEN_VM_DOES_NOT_SUPPORT_UTF-8", e);
        }
    }
}

Related

  1. encode(String input)
  2. encode(String raw)
  3. encode(String s)
  4. encode(String s)
  5. encode(String s)
  6. encode(String s)
  7. encode(String s)
  8. encode(String s)
  9. encode(String s, String enc)