How to remove lines from beginning and end of line (Java)?

I have a line containing some text, followed by an empty line. What is the best way to save part of the text, but remove the end of the line from the end?

+106
java string line blank-line
Sep 17 '11 at 11:17
source share
7 answers

Use the String.trim() method to get rid of spaces (spaces, newlines, etc.) from the beginning to the end of the line.

 String trimmedString = myString.trim(); 
+240
Sep 17 '11 at 11:19
source share
 String.replaceAll("[\n\r]", ""); 
+24
Sep 17 '11 at 11:51 on
source share

If your string is potentially null , consider using StringUtils.trim() - the null version of String.trim() .

+1
Oct 25 '17 at 23:13
source share
 String trimStartEnd = "\n TestString1 linebreak1\nlinebreak2\nlinebreak3\n TestString2 \n"; System.out.println("Original String : [" + trimStartEnd + "]"); System.out.println("-----------------------------"); System.out.println("Result String : [" + trimStartEnd.replaceAll("^(\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029])|(\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029])$", "") + "]"); 
  1. Start of line = ^,
  2. End of line = $,
  3. regular expression = | ,
  4. Line feed = \ r \ n | [\ n \ x0B \ x0C \ r \ u0085 \ u2028 \ u2029]
+1
Jul 18 '18 at 6:26
source share

I am going to add an answer to this question because, although I had the same question, the answer provided was not enough. Given some thoughts, I realized that this can be done very easily with regular expression.

To remove new lines from the beginning:

 // Trim left String[] a = "\n\nfrom the beginning\n\n".split("^\\n+", 2); System.out.println("-" + (a.length > 1 ? a[1] : a[0]) + "-"); 

and end of line:

 // Trim right String z = "\n\nfrom the end\n\n"; System.out.println("-" + z.split("\\n+$", 2)[0] + "-"); 

I am sure this is not the most efficient way to trim a string. But this is apparently the cleanest and easiest way to embed such an operation.

Note that the same method can be performed to trim any variations and combination of characters from either end, as this is a simple regular expression.

0
Mar 06 '17 at 7:14
source share

TL; dr

 String cleanString = dirtyString.strip() ; // Call new 'String::string' method. 

String::strip…

The old String::trim method has a weird space definition.

As discussed here , Java 11 adds new strip… methods to the String class. They use a more Unicode-friendly definition of spaces. See the rules for this definition in the JavaDoc class for Character::isWhitespace .

Sample code.

 String input = " some Thing "; System.out.println("before->>"+input+"<<-"); input = input.strip(); System.out.println("after->>"+input+"<<-"); 

Or you can remove only leading or trailing spaces.

You do not mention exactly which points in the code make up your new lines. I suppose your new line is probably included in this list of codes that strip is targeting

  • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR or PARAGRAPH_SEPARATOR), but it is also not an inextricable space ('\ u00A0', '\ u2007', '\ u202F').
  • This is '\ t', U + 0009 HORIZONTAL TAB.
  • This is '\ n', U + 000A LINE FEED.
  • This is '\ u000B', U + 000B VERTICAL TABLE.
  • This is '\ f', U + 000C FORM.
  • This is an 'R', U + 000D RETURN.
  • This is '\ u001C', FILE SEPARATOR U + 001C.
  • This is '\ u001D', U + 001D GROUP SEPARATOR.
  • This is "\ u001E", the record separator U + 001E.
  • This is '\ u001F', U + 0
0
May 20 '19 at
source share
 String text = readFileAsString("textfile.txt"); text = text.replace("\n", "").replace("\r", ""); 
-3
Jul 20 '14 at 6:44
source share



All Articles