# 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");
    }
}

🛠️ Auto-refactor Available

You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

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