Java удалить символы переноса строки

Как удалить символы новой строки из начала и конца строки (Java)?

У меня есть строка, содержащая некоторый текст, за которым следует пустая строка. Какой лучший способ сохранить часть текста, но удалить конец строки с конца?

5 ответов

Используйте String.trim() метод, чтобы избавиться от пробелов (пробелов, новых строк и т.д.) от начала и до конца строки.

String trimmedString = myString.trim(); 

Если вы хотите избавиться, скажем, от каждого переноса строки, вы можете использовать регулярные выражения.

Ах, начало и конец. Я вижу, я видел комментарий об избавлении от «каждого разрыва строки» и ответил на это. Моя вина

Bro, @JohnB Он также удалит все символы новой строки между строкой. просьба удалить только начальный и конечный символ новой строки.

Будьте осторожны, если вы зацикливаетесь. Это должно будет скомпилировать регулярное выражение при каждом вызове.

Если ваша строка потенциально null , рассмотрите возможность использования StringUtils.trim() — нулевой версии String.trim() .

Я собираюсь добавить ответ на этот вопрос, потому что, хотя у меня был тот же вопрос, предоставленного ответа не хватало. Учитывая некоторые мысли, я понял, что это можно сделать очень легко с регулярным выражением.

Чтобы удалить новые строки с самого начала:

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

Я уверен, что это не самый эффективный способ обрезки строки. Но это, по-видимому, самый чистый и самый простой способ встроить такую ​​операцию.

Обратите внимание, что этот же метод можно выполнить, чтобы обрезать любые вариации и комбинацию символов с любого конца, поскольку это простое регулярное выражение.

Да, но что, если вы не знаете, сколько строк в начале / конце? Ваше решение предполагает, что в обоих случаях есть ровно 2 символа новой строки

Второй параметр split() — это просто предел. Оставьте это, если вы хотите совпадать неограниченное количество раз.

String text = readFileAsString("textfile.txt"); text = text.replace("\n", "").replace("\r", ""); 

Это не правильно отвечает на вопрос. Он удаляет все CR и LF, а не только те, которые в начале и в конце.

Источник

How to remove line breaks from a file in Java?

How can I replace all line breaks from a string in Java in such a way that will work on Windows and Linux (ie no OS specific problems of carriage return/line feed/new line etc.)? I’ve tried (note readFileAsString is a function that reads a text file into a String):

String text = readFileAsString("textfile.txt"); text.replace("\n", ""); 

Oh, if you want to delete all linefeeds, remove all \n AND all \r (because Windows linebreak is \r\n).

Hey, FYI if you can want to replace simultaneous muti-linebreaks with single line break then you can use myString.trim().replaceAll(«[\n]<2,>«, «\n») Or replace with a single space myString.trim().replaceAll(«[\n]<2,>«, » «)

17 Answers 17

You need to set text to the results of text.replace() :

String text = readFileAsString("textfile.txt"); text = text.replace("\n", "").replace("\r", ""); 

This is necessary because Strings are immutable — calling replace doesn’t change the original String, it returns a new one that’s been changed. If you don’t assign the result to text , then that new String is lost and garbage collected.

As for getting the newline String for any environment — that is available by calling System.getProperty(«line.separator») .

+1, correct. As to the reason: String is immutable. The replace() method returns the desired result. Also see the API docs: java.sun.com/javase/6/docs/api/java/lang/… Edit: ah you already edited that yourself in afterwards 🙂

Perhaps text = text.replace(«\r\n», » «).replace(«\n», » «); is a better solution: otherwise words will be «glued» to each other (without the single-space replacement).

You could also use square brackets to match newlines properly for any OS: .replaceAll(«[\\r\\n]+», «»)

As the question is asking for replacing ALL occurrences, the solution is rather text = text.replaceAll(«\n», «»).replaceAll(«\r», «»);

As noted in other answers, your code is not working primarily because String.replace(. ) does not change the target String . (It can’t — Java strings are immutable!) What replace actually does is to create and return a new String object with the characters changed as required. But your code then throws away that String .

Here are some possible solutions. Which one is most correct depends on what exactly you are trying to do.

Simply removes all the newline characters. This does not cope with Windows or Mac line terminations.

// #2 text = text.replace(System.getProperty("line.separator"), ""); 

Removes all line terminators for the current platform. This does not cope with the case where you are trying to process (for example) a UNIX file on Windows, or vice versa.

Removes all Windows, UNIX or Mac line terminators. However, if the input file is text, this will concatenate words; e.g.

So you might actually want to do this:

// #4 text = text.replaceAll("\\r\\n|\\r|\\n", " "); 

which replaces each line terminator with a space 1 . Since Java 8 you can also do this:

And if you want to replace multiple line terminator with one space:

1 — Note there is a subtle difference between #3 and #4. The sequence \r\n represents a single (Windows) line terminator, so we need to be careful not to replace it with two spaces.

Источник

How to remove newlines from beginning and end of a string?

I have a string that contains some text followed by a blank line. What’s the best way to keep the part with text, but remove the whitespace newline from the end?

12 Answers 12

Use String.trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.

String trimmedString = myString.trim(); 

Bro, @JohnB It will remove all the new line character in between the string as well. the ask is to remove only the leading & trailing new line character.

This Java code does exactly what is asked in the title of the question, that is «remove newlines from beginning and end of a string-java»:

String.replaceAll("^[\n\r]", "").replaceAll("[\n\r]$", "") 

Remove newlines only from the end of the line:

Remove newlines only from the beginning of the line:

Could you provide extra context to your answer? That way everyone can understand what your code does and why.

This is the correct solution because it only removes newlines and not spaces, tabs, or other whitespace characters.

tl;dr

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

String::strip…

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

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

Or you can strip just the leading or just the trailing whitespace.

You do not mention exactly what code point(s) make up your newlines. I imagine your newline is likely included in this list of code points targeted by strip :

  • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
  • It is '\t', U+0009 HORIZONTAL TABULATION.
  • It is '\n', U+000A LINE FEED.
  • It is '\u000B', U+000B VERTICAL TABULATION.
  • It is '\f', U+000C FORM FEED.
  • It is '\r', U+000D CARRIAGE RETURN.
  • It is '\u001C', U+001C FILE SEPARATOR.
  • It is '\u001D', U+001D GROUP SEPARATOR.
  • It is '\u001E', U+001E RECORD SEPARATOR.
  • It is '\u001F', U+0

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

If you only want to remove line breaks (not spaces, tabs) at the beginning and end of a String (not inbetween), then you can use this approach:

Use a regular expressions to remove carriage returns ( \\r ) and line feeds ( \\n ) from the beginning ( ^ ) and ending ( $ ) of a string:

public class RemoveLineBreaks < public static void main(String[] args) < var s = "\nHello world\nHello everyone\n"; System.out.println("before: >"+s+"<"); s = s.replaceAll("(^[\\r\\n]+|[\\r\\n]+$)", ""); System.out.println("after: >"+s+" <"); >> 
before: > Hello world Hello everyone < after: >Hello world Hello everyone 

I'm going to add an answer to this as well because, while I had the same question, the provided answer did not suffice. Given some thought, I realized that this can be done very easily with a regular expression.

To remove newlines 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]) + "-"); 
// Trim right String z = "\n\nfrom the end\n\n"; System.out.println("-" + z.split("\\n+$", 2)[0] + "-"); 

I'm certain that this is not the most performance efficient way of trimming a string. But it does appear to be the cleanest and simplest way to inline such an operation.

Note that the same method can be done to trim any variation and combination of characters from either end as it's a simple regex.

Yeah but what if you don't know how many lines are at the beginning/end? Your solution assumes there are exactly 2 newlines in both cases

The second parameter of split() is just the limit. Leave it off if you want to match an unlimited number of times.

function replaceNewLine(str) < return str.replace(/[\n\r]/g, ""); >
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 a string = ^ ,
  2. End of a string = $ ,
  3. regex combination = | ,
  4. Linebreak = \r\n|[\n\x0B\x0C\r\u0085\u2028\u2029]
String myString = "\nLogbasex\n"; myString = org.apache.commons.lang3.StringUtils.strip(myString, "\n"); 

For anyone else looking for answer to the question when dealing with different linebreaks:

string.replaceAll("(\n|\r|\r\n)$", ""); // Java 7 string.replaceAll("\\R$", ""); // Java 8 

This should remove exactly the last line break and preserve all other whitespace from string and work with Unix (\n), Windows (\r\n) and old Mac (\r) line breaks: https://stackoverflow.com/a/20056634, https://stackoverflow.com/a/49791415. "\\R" is matcher introduced in Java 8 in Pattern class: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

// Windows: value = "\r\n test \r\n value \r\n"; assertEquals("\r\n test \r\n value ", value.replaceAll("\\R$", "")); // Unix: value = "\n test \n value \n"; assertEquals("\n test \n value ", value.replaceAll("\\R$", "")); // Old Mac: value = "\r test \r value \r"; assertEquals("\r test \r value ", value.replaceAll("\\R$", "")); 

Источник

Remove end of line characters from Java string

I want remove \r and \n from String(hello\r\njava\r\nbook) . I want the result to be "hellojavabook" . How can I do this?

12 Answers 12

If you only want to remove \r\n when they are pairs (the above code removes either \r or \n) do this instead:

I would recommend you used System.getProperty("line.separator"); , instead of hardcoded line breakers. Each OS has its own line separator.

@GGrec sometimes the text String / File was not made by the same OS. So System.getProperty("line.separator"); won't work in these cases. But yes, if you can trust the same-OS origin, it is better to use the system property.

How would you use the value returned for line.separator ? I tried passing "\\" + lineSeparator in the regex and it did not work

public static void main(final String[] argv)

It would be useful to add the tabulation in regex too.

If you want to avoid the regex, or must target an earlier JVM, String.replace() will do:

And to remove a CRLF pair:

The latter is more efficient than building a regex to do the same thing. But I think the former will be faster as a regex since the string is only parsed once.

str = str.replaceAll("\\\\r","") str = str.replaceAll("\\\\n","") 

You can either directly pass line terminator e.g. \n, if you know the line terminator in Windows, Mac or UNIX. Alternatively you can use following code to replace line breaks in either of three major operating system.

Above code line will replace line breaks with space in Mac, Windows and Linux. Also you can use line-separator. It will work for all OS. Below is the code snippet for line-separator.

String lineSeparator=System.lineSeparator(); String newString=yourString.replace(lineSeparator, ""); 

Источник

Читайте также:  Java temp file deleteonexit
Оцените статью