String (@NotNull String text) { if (text.isEmpty()) return text; // Handle the case when \r and \n are in different chunks: "text1 \r" and "\n text2" boolean preserveFirstLF = text.startsWith(LineSeparator.LF.getSeparatorString()) && myLastCR; boolean preserveLastCR = text.endsWith(LineSeparator.CR.getSeparatorString()); myLastCR = preserveLastCR; String textToConvert = text.substring(preserveFirstLF ? 1 : 0, preserveLastCR ? text.length() - 1 : text.length()); String textCRLF = StringUtil.convertLineSeparators(textToConvert, LineSeparator.CRLF.getSeparatorString()); if (preserveFirstLF) { textCRLF = LineSeparator.LF.getSeparatorString() + textCRLF; } if (preserveLastCR) { textCRLF += LineSeparator.CR.getSeparatorString(); } return textCRLF; }