What method would you suggest to normalizing a text in Java, for example
String raw = " This is\n a test\n\r ";
String txt = normalize(raw);
assert txt == "This is a test";
I'm thinking about StringUtils .replace() and .strip() methods, but maybe there is some easier way.
Try the following if it is just a matter of whitespaces
String txt = raw.replaceAll("\\s+", " ").trim();
Apache commons finally added this function: org.apache.commons.lang3.StringUtils.normalizeSpace(String str) // docs
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