Trying to remove any extra empty lines beyond 2 empty lines in a file made from a StringBuilder. The code is in groovy so I seem to not understand why simple java procedure don't seem to work. Tried 3 things... Any suggestions? See code snippets below
Regex doesn't do anything.
private static final removeExtraLines1(StringBuilder text)
{
String twoLines = "MIAW";
Pattern extraLines = Pattern.compile("\n")
text.replaceAll(extraLines, twoLines);
}
My own method that deleteCharAt doesn't work too?
private void removeExtraLines(StringBuilder text)
{
int i = 0;
while(i < text.length()-1)
{
if(text.charAt(i) == System.lineSeparator() && text.charAt(i++) == System.lineSeparator())
{
text.deleteCharAt(i++);
System.out.println("REPLACED AN EMPTY LINE");
}
}
}
Modifying the toString() method says regex method replaceAll is deprecated.
@Override
String toString()
{
def padding = " " * (4 * (level - 1) )
padding + "cat level $level [$title]: " +
contained.collect {
it.toPaddedString(padding) }
////My Code
String twoLines = System.lineSeparator()+System.lineSeparator();
Pattern extraLines = Pattern.compile(System.lineSeparator()+ System.lineSeparator()+ System.lineSeparator()+System.lineSeparator())
_toString().replaceAll(extraLines, twoLines)
}