سؤال

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