Java String Encode encodeString(String s)

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

Description

Encodes string to be URL safe.

License

GNU General Public License

Parameter

Parameter Description
s string to be encoded

Return

encoded string

Declaration

public static String encodeString(String s) 

Method Source Code


//package com.java2s;
/*/*from   w  ww . j a v  a2 s.c  o m*/
 * Copyright (C) 2002-2014 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Main {
    /**
     * Encodes string to be URL safe. For encoding URL use encodeURL
     * method.
     *
     * @param s string to be encoded
     * @return encoded string
     */
    public static String encodeString(String s) {
        try {
            return URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("string encoding failed", e);
        }
    }
}

Related

  1. encodeString(final String string)
  2. encodeString(OutputStream out, String str)
  3. encodeString(String b)
  4. encodeString(String in)
  5. encodeString(String myString)
  6. encodeString(String s)
  7. encodeString(String s, boolean useUnicode)
  8. encodeString(String s, Integer size)
  9. encodeString(String s, String charset)