Drop newline characters in a string (\ r \ n \ t) in Java

Source: Internet
Author: User
Tags one more line

Let's take a look at a few examples:

Example 1:

public class Test {

public static void Main (string[] args) {


String s = "' SDS GDASDA" + "\ n" + "Edaeafd '";
System.out.println ("Before conversion:" +s);


s = S.replaceall ("\r|\n", "");


System.out.println ("After conversion:" +s);
}
}
Output results:

Before conversion: ' SDS GDASDA
EDAEAFD '
After conversion: ' SDS GDASDAEDAEAFD '

Looking at an example:

Example 2:

System.out.println ("\ R output:" + "abc" + "\ r" + "abc");
SYSTEM.OUT.PRINTLN ("\\n Output:" + "abc" + "\ n" + "abc");

The above two sentences are in the same format as the console output:

\ r output: ABC
Abc
\ r output: ABC
Abc

So does it mean that \ r is equal to \ n?

Example 3:

public class Test {

public static void Main (string[] args) {


String s = "' SDS GDASDA" + "\ n" + "Edaeafd '";
System.out.println ("Before conversion:" +s);


s = S.replaceall ("\ R", "");


System.out.println ("After conversion:" +s);
}
}

Before conversion: ' SDS GDASDA
EDAEAFD '
After conversion: ' SDS GDASDA
EDAEAFD '

The output shows that \ r is not equal to \ n. hi.

So what's the difference between them?

Example 4:

public class Test {

public static void Main (string[] args) {


String s = "' SDS GDASDA" + "\n\r" + "Edaeafd '";

System.out.println ("Before conversion:" +s);


s = S.replaceall ("\r|\n", "");


System.out.println ("After conversion:" +s);
}
}

Output results:

Before conversion: ' SDS GDASDA

EDAEAFD '
After conversion: ' SDS GDASDAEDAEAFD '

You can see that \ r represents a carriage return, \ n represents a second line (\ r) called carriage return Carriage return; \ n = new line \ ew Line)

We can do in one experiment:

Example 5:

public class Test {

public static void Main (string[] args) {


String s = "' SDS GDASDA" + "\ r \ n" + "Edaeafd '";

System.out.println ("Before conversion:" +s);


s = S.replaceall ("\r|\n", "");


System.out.println ("After conversion:" +s);
}
}

Output results:

Before conversion: ' SDS GDASDA
EDAEAFD '
After conversion: ' SDS GDASDAEDAEAFD ' in

In Example 4 to Example 5, we just changed the position of the \n\r of the string s to \ r \ n, but found that the example 4 output resulted in one more line of space.

As for why example 4 and example 5 output will have this difference is the difference between \ r \ n and \n\r, this is a better answer on the net:

Enter+newline with different platforms:
Windows:   \ r \ n
Mac:           \ r
Unix/linux:  \ n

In "abc" + "\n\rdef", \n\r don't   match any platform,so it is considered as \ r  (which match unix/linux  and Mac), so there are two new lines.
In  "abc" + "\r\ndef"   \ r \ n matches the Windows Platform,so it is considered as only one new line.
SCIM crushed, so just 中文版.

About \ t It is equivalent to pressing the TAB key.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.