This method encodes the URL, removes the spaces from the URL and replaces the same with "%20". - Java Network

Java examples for Network:URL

Description

This method encodes the URL, removes the spaces from the URL and replaces the same with "%20".

Demo Code

/**//w  ww . j  ava  2 s.  c  om
 * Copyright (c) 2005-2006 Aptana, Inc.
 *
 * 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. If redistributing this code,
 * this entire header must remain intact.
 */
//package com.java2s;

public class Main {
    /**
     * This method encodes the URL, removes the spaces from the URL and replaces the same with <code>"%20"</code>.
     * This method is required to fix Bug 77840.
     * 
     * @param input
     * @return String
     * @since 3.0.2
     */
    public static String urlEncodeForSpaces(char[] input) {

        if (input == null) {
            return null;
        }

        StringBuffer retu = new StringBuffer(input.length);
        for (int i = 0; i < input.length; i++) {
            if (input[i] == ' ') {
                retu.append("%20"); //$NON-NLS-1$
            } else {
                retu.append(input[i]);
            }
        }
        return retu.toString();
    }
}

Related Tutorials