Returns a string with visible whitespace characters, removing the invisible ones. - Java java.lang

Java examples for java.lang:String Whitespace

Description

Returns a string with visible whitespace characters, removing the invisible ones.

Demo Code


//package com.java2s;

public class Main {

    public static final String SPACE_SYMBOL = "\u00b7";
    public static final String TAB_SYMBOL = "\u00bb";
    public static final String CARRIAGE_RETURN_SYMBOL = "\u00a4";
    public static final String LINE_FEED_SYMBOL = "\u00b6";

    /**/*from  ww w .jav  a2  s. c  o m*/
     * Returns a string with visible whitespace characters, removing the invisible ones.
     * <p>
     * It's replaced carriage return, line feed, tabs and spaces with visible (replacement) characters.
     *
     * @param string the source string
     * @return the new string with visible new line characters
     */
    public static String replaceWithVisibleWhiteSpaceChars(String string) {
        return string.replaceAll("\r", CARRIAGE_RETURN_SYMBOL)
                .replaceAll("\n", LINE_FEED_SYMBOL)
                .replaceAll("\\t", TAB_SYMBOL)
                .replaceAll(" ", SPACE_SYMBOL);
    }
}

Related Tutorials