Is there a better way of doing this in Java8 e.g. with Stream.of(...)?
if (messageBody.startsWith("\n\n")) {
messageBody = messageBody.replaceFirst("\n", "");
}
final String messageBodyFormatted = messageBody.replaceAll("\n", "<br />");
Here’s an option:
if (messageBody.startsWith("\n\n")) {
messageBody = messageBody.substring(1); // remove first newline
}
final String messageBodyFormatted = messageBody.replace("\n", "<br />");
Inside the if statement I’m simply removing the first char since I already know what it is. If you think it’s subtle, just use your own version instead. I changed the final replaceAll() with replace() — in spite of the name it does the same, only without regular expressions.
replaceFirst("\n(?=\n)", "");. I don't think Java 8 will give you more here.startsWithcheck which requires the occurrence to be at the very start of the string. Fixing that, allows an even simpler solution:replaceFirst("^\n\n", "\n").