문제

I want to replace a carriage return followed by quotation marks with just quotation marks. For example, if I have:

Hello World
"Hello World"

I would like the result to be:

Hello World"Hello World"

This is my attempt, where String text is what I have above:

String adjusted = text.replaceAll("[\n][\"], "\"");

However, my IDE does not accept this. Thanks for the help!

도움이 되었습니까?

해결책 2

You can use replace instead of replaceAll to avoid matching regular expression, but instead matching literals.

String adjusted = text.replace("\n\"", "\"");

If you want this method to use you operating system line separators you should use

String adjusted = text.replace(System.lineSeparator()+"\"", "\"");

다른 팁

String adjusted = text.replaceAll("(?m)\r?\n\"", "\"");

The (?m) is for multi-line usage, for \r for a real CR in Windows (CR+LF).

You should do it in a platform agnostic way like:

String newline = System.getProperty("line.separator");
String newStr = str.replaceAll(newline, "\"");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top