Java Array to String arrayToString(Object[] anArray, String aPrefix, String aSeparator, String aSuffix, String anEscapeChars, char anEscapeSymbol)

Here you can find the source of arrayToString(Object[] anArray, String aPrefix, String aSeparator, String aSuffix, String anEscapeChars, char anEscapeSymbol)

Description

array To String

License

Open Source License

Parameter

Parameter Description
anArray The array to convert. If `null` or empty, only the prefix and suffix are concatenated.
aPrefix The prefix to delimit the beginning of the array with. Omitted entirely if `null`.
aSeparator The character sequence to separate elements in the array with. Defaults to a single space (' ') if `null`.
aSuffix The suffix to delimit the end of the array with. Omitted entirely if `null`.
anEscapeChars A string containing the characters that need to be escaped in the elements. Note that characters in the prefix, suffix and separator are not escaped. No escaping is done if `null` or empty.
anEscapeSymbol The symbol to be inserted before characters that need to be escaped. May be used to escape itself.

Return

A string representation of the array. This value is never `null`, although it may be empty if the array, prefix and suffix are empty.

Declaration

public static String arrayToString(Object[] anArray, String aPrefix, String aSeparator, String aSuffix,
        String anEscapeChars, char anEscapeSymbol) 

Method Source Code

//package com.java2s;
/*/*from  ww w  . j a va2  s.  c  o  m*/
 * TextUtilities.java (Class: com.madphysicist.tools.util.TextUtilities)
 *
 * Mad Physicist JTools Project (General Purpose Utilities)
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 by Joseph Fox-Rabinovitz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * @brief Converts an array of objects into a string.
     *
     * Non-string elements will be converted to strings using their `toString()`
     * method. This method does not currently support primitive or nested
     * arrays.
     *
     * The return value has the following format:
     *
     *      <arrayPrefix><escaped element><arraySeparator> ... <arraySuffix>
     *
     * The array separator does not appear after the last element.
     *
     * Elements will have all characters that appear in `anEscapeChars` escaped
     * by preceding them with the escape symbol. The prefix, suffix and
     * separator and entry separator will not be escaped. The recommended use of
     * this method is to add the symbols in the prefix, suffix and separator to
     * the escape characters so that they can be identified unambiguously.
     *
     * To convert primitive arrays to arrays of string objects that can be
     * processed by this method, use the appropriate methods of the
     * `ArrayUtilities` class.
     *
     * @param anArray The array to convert. If `null` or empty, only the prefix
     * and suffix are concatenated.
     * @param aPrefix The prefix to delimit the beginning of the array with.
     * Omitted entirely if `null`.
     * @param aSeparator The character sequence to separate elements in the
     * array with. Defaults to a single space (' ') if `null`.
     * @param aSuffix The suffix to delimit the end of the array with. Omitted
     * entirely if `null`.
     * @param anEscapeChars A string containing the characters that need to be
     * escaped in the elements. Note that characters in the prefix, suffix and
     * separator are not escaped. No escaping is done if `null` or empty.
     * @param anEscapeSymbol The symbol to be inserted before characters that
     * need to be escaped. May be used to escape itself.
     * @return A string representation of the array. This value is never `null`,
     * although it may be empty if the array, prefix and suffix are empty.
     * @see ArrayUtilities
     * @since 3.0.0
     */
    public static String arrayToString(Object[] anArray, String aPrefix, String aSeparator, String aSuffix,
            String anEscapeChars, char anEscapeSymbol) {
        if (aPrefix == null)
            aPrefix = "";
        if (aSeparator == null)
            aSeparator = " ";
        if (aSuffix == null)
            aSuffix = "";

        if (anArray == null || anArray.length == 0)
            return aPrefix + aSuffix;

        StringBuilder sb = new StringBuilder();

        sb.append(aPrefix);
        for (int i = 0; i < anArray.length; i++) {
            if (i > 0)
                sb.append(aSeparator);
            sb.append(escapeString(anArray[i].toString(), anEscapeChars, anEscapeSymbol));
        }
        sb.append(aSuffix);
        return sb.toString();
    }

    /**
     * Escapes the specified characters of a string by prepending an escape
     * symbol. Only one pass is made through the string, so that the escape
     * symbol may safely be present in the list of characters to escape.
     *
     * @param string the string to escape.
     * @param escapeChars the characters to escape. Every occurrence of any of
     * the characters in this string is prepended with the escape symbol. If
     * this argument is empty or null, this method does nothing.
     * @param escapeSymbol a symbol used to escape the required characters.
     * @return a string similar to the input but with all of the characters in
     * {@code escapeChars} preceded by an escape sequence
     * @since 1.0.0
     */
    public static String escapeString(String string, String escapeChars, char escapeSymbol) {
        if (string == null || string.isEmpty() || escapeChars == null || escapeChars.isEmpty()) {
            return string;
        }
        StringBuilder sb = new StringBuilder(string.length());
        for (int index = 0; index < string.length(); index++) {
            char ch = string.charAt(index);
            if (escapeChars.indexOf(ch) >= 0)
                sb.append(escapeSymbol);
            sb.append(ch);
        }
        return sb.toString();
    }
}

Related

  1. arrayToString(Object obj)
  2. arrayToString(Object obj[])
  3. arrayToString(Object... keys)
  4. arrayToString(Object... objects)
  5. arrayToString(Object[] a)
  6. arrayToString(Object[] args, String separator)
  7. arrayToString(Object[] arr)
  8. arrayToString(Object[] arr)
  9. arrayToString(Object[] arr)