Java URL Encode encodeArray(final String[] val)

Here you can find the source of encodeArray(final String[] val)

Description

Return a String representing the given String array, achieved by URLEncoding the individual String elements then concatenating with intervening blanks.

License

Apache License

Parameter

Parameter Description
val String[] value to encode

Return

String encoded value

Declaration

public static String encodeArray(final String[] val) 

Method Source Code

//package com.java2s;
/* ********************************************************************
Licensed to Jasig under one or more contributor license
agreements. See the NOTICE file distributed with this work
for additional information regarding copyright ownership.
Jasig licenses this file to you under the Apache License,
Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a
copy of the License at:/*from   w  w w .  j av  a  2  s  .co  m*/
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

import java.net.URLEncoder;

public class Main {
    /** Return a String representing the given String array, achieved by
     * URLEncoding the individual String elements then concatenating with
     *intervening blanks.
     *
     * @param  val    String[] value to encode
     * @return String encoded value
     */
    public static String encodeArray(final String[] val) {
        if (val == null) {
            return null;
        }

        int len = val.length;

        if (len == 0) {
            return "";
        }

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < len; i++) {
            if (i > 0) {
                sb.append(" ");
            }

            String s = val[i];

            try {
                if (s == null) {
                    sb.append("\t");
                } else {
                    sb.append(URLEncoder.encode(s, "UTF-8"));
                }
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }

        return sb.toString();
    }
}

Related

  1. encode(String value)
  2. encode(String value)
  3. encode(String value)
  4. encode(String value, String charset)
  5. encode(String value, String encoding)
  6. encodeForUrl(final String s)
  7. encodeSrcUrl(String fullUri)
  8. encodeStringURL(String str)
  9. encodeUri(String url)