Java String Replace replaceNonPrintableAsciiCharacters(String str)

Here you can find the source of replaceNonPrintableAsciiCharacters(String str)

Description

Replaces the non-printable ASCII characters with readable counterparts in square brackets, e.g.

License

Open Source License

Parameter

Parameter Description
str the String in which to replace the characters

Return

a printable String

Declaration

public static String replaceNonPrintableAsciiCharacters(String str) 

Method Source Code

//package com.java2s;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;

public class Main {
    private static final String[] replacements = new String[256];

    /**//from  w  ww  . java  2s .c  o m
     * Replaces the non-printable ASCII characters with readable counterparts in square brackets, e.g. \0x00 -> [NUL].
     *
     * @param str the String in which to replace the characters
     *
     * @return a printable String
     */
    public static String replaceNonPrintableAsciiCharacters(String str) {
        byte[] bytes = str.getBytes();
        return replaceNonPrintableAsciiCharacters(bytes, 0, bytes.length);
    }

    /**
     * Same as calling {@link StringUtils#replaceNonPrintableAsciiCharacters(String)} with argument {@code new
     * String(bytes)}.
     *
     * @param bytes the byte array to transfer to a string and replace non-printable characters in
     *
     * @return a printable string
     */
    public static String replaceNonPrintableAsciiCharacters(byte[] bytes) {
        return replaceNonPrintableAsciiCharacters(bytes, 0, bytes.length);
    }

    /**
     * Same as calling {@link StringUtils#replaceNonPrintableAsciiCharacters(String)} with argument {@code new
     * String(bytes, 0, bytes.length)}.
     *
     * @param bytes  the byte array to transfer to a string and replace non-printable characters in
     * @param offset the offset in {@code bytes} from which to start constructing the string
     * @param length the number of bytes to use for constructing the string
     *
     * @return a printable string
     */
    public static String replaceNonPrintableAsciiCharacters(byte[] bytes, int offset, int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = offset; i < offset + length; i++) {
            builder.append(replacements[128 + bytes[i]]);
        }
        return builder.toString();
    }

    public static String toString(short[] l, int offset, int length) {
        LinkedList<Short> ll = new LinkedList<Short>();
        for (int i = offset; i < offset + length; ++i) {
            ll.add(l[i]);
        }

        return toString(ll, ", ");
    }

    public static String toString(Collection l, String divider) {
        StringBuilder b = new StringBuilder();

        if (l == null) {
            return "<null>";
        }

        for (Object o : l) {
            String t = o != null ? o.toString() : "{null}";
            if (b.length() > 0) {
                b.append(divider);
            }

            b.append(t);
        }

        return b.toString().trim();
    }

    public static String toString(Collection<?> list) {
        if (list == null) {
            return "null";
        }

        return Arrays.toString(list.toArray());
    }
}

Related

  1. replaceAll(String source, String toReplace, String replacement)
  2. replaceEmitKeys(String map, Collection newKeys)
  3. replaceFirst(String input, String search, String replacement)
  4. replaceIgnoreCase(String line, String oldString, String newString)
  5. replaceIgnoreCase(String source, String strBeReplace, String strReplaced)
  6. replaceOnce(String template, String placeholder, String replacement)
  7. replaceSign(String sourceStr, char startc, char endc, String replaceStr)
  8. replaceSpace(String str, int actualLength)
  9. replaceUnsafeNamespaceDelimiters(String input)