final String remove = " " // tab is 3 spaces
while (lineOfText != null)
{
if (lineOfText.contains(remove))
{
lineOfText = " ";
}
outputFile.println(lineOfText);
lineOfText = inputFile.readLine();
}
I tried running this but it doesn't replace the tabs with one blank space. Any solutions?
Tab is not three spaces. It's a special character that you obtain with an escape, specifically final String remove = "\t";
and
if (lineOfText.contains(remove))
lineOfText = lineOfText.replaceAll(remove, " ");
}
or remove the if
(because replaceAll
doesn't need it) like,
lineOfText = lineOfText.replaceAll(remove, " ");
You can simply use this regular expression to replace any type of escapes( including tabs, newlines, spaces etc.) within a String with the desired one:
lineOfText.replaceAll("\\s", " ");
Here in this example in the string named lineOfText we have replaced all escapes with whitespaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With