Android String Replace collapseControlChars(String str, String replacement)

Here you can find the source of collapseControlChars(String str, String replacement)

Description

Returns a string with all sequences of ISO control chars (0x00 to 0x1F and 0x7F to 0x9F) replaced by the supplied string.

License

Apache License

Parameter

Parameter Description
str the string you want to strip of ISO control chars
replacement the replacement string

Return

a String with all control characters replaced by the replacement string, or null if input is null.

Declaration

@Deprecated
public static String collapseControlChars(String str, String replacement) 

Method Source Code

//package com.java2s;
/*/*from   www  .jav  a 2 s  .c om*/
 * Copyright (C) 2000 Google Inc.
 *
 * Licensed 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
 *
 * 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.
 */

public class Main {
    /**
     * Returns a string with all sequences of ISO control chars (0x00 to 0x1F and
     * 0x7F to 0x9F) replaced by the supplied string.  ISO control characters are
     * identified via {@link Character#isISOControl(char)}.
     *
     * @param str the string you want to strip of ISO control chars
     * @param replacement the replacement string
     * @return a String with all control characters replaced by the replacement
     * string, or null if input is null.
     * @deprecated use {@link CharMatcher#JAVA_ISO_CONTROL}. If {@code
     *     replacement} is the empty string, use {@link
     *     CharMatcher#removeFrom(CharSequence)}; if it is a single character,
     *     use {@link CharMatcher#collapseFrom(CharSequence, char)}; for longer
     *     replacement strings use
     *     {@code str.replaceAll("\p{Cntrl}+", replacement)}.
     *     In all cases you must first ensure that {@code str} is not null.
     */
    @Deprecated
    public static String collapseControlChars(String str, String replacement) {
        /*
         * We re-implement the StringUtil.collapse() loop here rather than call
         * collapse() with an input String of control chars, because matching via
         * isISOControl() is about 10x faster.
         */
        if (str == null) {
            return null;
        }

        StringBuilder newStr = new StringBuilder();

        boolean prevCharMatched = false;
        char c;
        for (int i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            if (Character.isISOControl(c)) {
                // this character is matched
                if (prevCharMatched) {
                    // apparently a string of matched chars, so don't append anything
                    // to the string
                    continue;
                }
                prevCharMatched = true;
                newStr.append(replacement);
            } else {
                prevCharMatched = false;
                newStr.append(c);
            }
        }

        return newStr.toString();
    }

    /**
     * @return a string representation of the given native array.
     */
    public static String toString(float[] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append(iArray[i]);
            if (i != (iArray.length - 1)) {
                buffer.append(", ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a string representation of the given native array.
     */
    public static String toString(long[] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append(iArray[i]);
            if (i != (iArray.length - 1)) {
                buffer.append(", ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a string representation of the given native array
     */
    public static String toString(int[] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append(iArray[i]);
            if (i != (iArray.length - 1)) {
                buffer.append(", ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a string representation of the given array.
     */
    public static String toString(String[] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append("'").append(iArray[i]).append("'");
            if (i != iArray.length - 1) {
                buffer.append(", ");
            }
        }
        buffer.append("]");

        return buffer.toString();
    }

    /**
     * Returns the string, in single quotes, or "NULL". Intended only for
     * logging.
     *
     * @param s the string
     * @return the string, in single quotes, or the string "null" if it's null.
     */
    public static String toString(String s) {
        if (s == null) {
            return "NULL";
        } else {
            return new StringBuilder(s.length() + 2).append("'").append(s)
                    .append("'").toString();
        }
    }

    /**
     * @return a string representation of the given native array
     */
    public static String toString(int[][] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append("[");
            for (int j = 0; j < iArray[i].length; j++) {
                buffer.append(iArray[i][j]);
                if (j != (iArray[i].length - 1)) {
                    buffer.append(", ");
                }
            }
            buffer.append("]");
            if (i != iArray.length - 1) {
                buffer.append(" ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a string representation of the given native array.
     */
    public static String toString(long[][] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append("[");
            for (int j = 0; j < iArray[i].length; j++) {
                buffer.append(iArray[i][j]);
                if (j != (iArray[i].length - 1)) {
                    buffer.append(", ");
                }
            }
            buffer.append("]");
            if (i != iArray.length - 1) {
                buffer.append(" ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a String representation of the given object array.
     * The strings are obtained by calling toString() on the
     * underlying objects.
     */
    public static String toString(Object[] obj) {
        if (obj == null) {
            return "NULL";
        }
        StringBuilder tmp = new StringBuilder();
        tmp.append("[");
        for (int i = 0; i < obj.length; i++) {
            tmp.append(obj[i].toString());
            if (i != obj.length - 1) {
                tmp.append(",");
            }
        }
        tmp.append("]");
        return tmp.toString();
    }
}

Related

  1. replace(String s, char oldSub, char newSub)
  2. replace(String source, CharSequence target, CharSequence replacement)
  3. replaceChar(String source, String subject, String object)
  4. replaceSpaceCharacter(String string)
  5. collapse(String str, String chars, String replacement)
  6. stringReplace(String str, String what, String replacement)