Returns a string with visible new line characters, shown along the invisible ones. - Java java.lang

Java examples for java.lang:String New Line

Description

Returns a string with visible new line characters, shown along the invisible ones.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String string = "java2s.com";
        System.out.println(addVisibleNewLineChars(string));
    }/*from   w w w . ja  v a2  s  .  c  o m*/

    public static final String CARRIAGE_RETURN_SYMBOL = "\u00a4";
    public static final String LINE_FEED_SYMBOL = "\u00b6";

    /**
     * Returns a string with visible new line characters, shown along the invisible ones.
     *
     * @param string the source string
     * @return the new string with visible new line characters
     */
    public static String addVisibleNewLineChars(String string) {
        return string.replaceAll("\r", CARRIAGE_RETURN_SYMBOL + "\r")
                .replaceAll("\n", LINE_FEED_SYMBOL + "\n");
    }
}

Related Tutorials