Home > Refactorings > Refactorings that work with strings – Use Environment.NewLine

Refactorings that work with strings – Use Environment.NewLine

November 25th, 2011

The Use Environment.NewLine refactoring is one of the simplest refactoring shipped in DevExpress Refactor! Pro which improves code portability. This refactoring replaces the “\r\n” string with the value of the Environment.NewLine property reference. The Environment.NewLine is a static string property from the System namespace that is tied to the current executing environment (platform). It returns a valid “line feed/carriage return” string that corresponds to the current operating system, for example: “\r\n” for Windows platforms, or a string containing just a line feed (“\n“) for Unix platforms.

You may use this refactoring to improve the portability of the code and readability. Consider the following code:

Refactorings - Use Environment.NewLine sample code

Applying this refactoring will help you replace all “\r\n” occurrences:

Refactorings - Use Environment.NewLine preview

Resulting code:

Refactorings - Use Environment.NewLine result

Note if you have CodeRush Pro installed and the Code Issues feature turned on, you will see the special highlighting for the cases where the Use Environment.NewLine refactoring can be applied:

CodeRush code issues - Environment NewLine can be used

To learn more about the “Environment.NewLine can be used” code issue, please read the appropriate topic.

Also, refer to other refactorings that work with strings.

Products: Refactor! Pro
Versions: 11.1 and up
VS IDEs: any
Updated: Dec/02/2011
ID: R046

Similar Posts: