# Use Portable Newline

# Description

This rule replaces any occurrences of '\n' with '%n'.
For example 'String.format("\n\n")' is replaced by 'String.format("%n%n")'.

# Benefits

The benefit of this approach is that '%n' is portable across platforms.

# Tags

# Code Changes

Pre

public class TestStringFormatLineSeparatorRule {
    public String testStringFormatLineSeparator01() {
        return String.format("\n\n");
    }
    public String testStringFormatLineSeparator02() {
        return String.format(Locale.GERMAN, "\n\n");
    }
    public String testStringFormatLineSeparator03() {
        return String.format("\r\n\r\n");
    }
    public String testStringFormatLineSeparator04() {
        return String.format(Locale.GERMAN, "\r\n\r\n%n");
    }
}

Post


public class TestStringFormatLineSeparatorRule {
    public String testStringFormatLineSeparator01() {
        return String.format("%n%n");
    }
    public String testStringFormatLineSeparator02() {
        return String.format(Locale.GERMAN, "%n%n");
    }
    public String testStringFormatLineSeparator03() {
        return String.format("%n%n");
    }
    public String testStringFormatLineSeparator04() {
        return String.format(Locale.GERMAN, "%n%n%n");
    }
}

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

Property Value
Rule ID StringFormatLineSeparator
First seen in jSparrow version 1.0.0
Minimum Java version 5
Remediation cost 1 min
Links