Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize/polish a text in Java?

Tags:

java

string

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.

like image 739
yegor256 Avatar asked Mar 17 '26 20:03

yegor256


2 Answers

Try the following if it is just a matter of whitespaces

String txt = raw.replaceAll("\\s+", " ").trim();
like image 87
Yaneeve Avatar answered Mar 19 '26 16:03

Yaneeve


Apache commons finally added this function: org.apache.commons.lang3.StringUtils.normalizeSpace(String str) // docs

like image 43
AlexV Avatar answered Mar 19 '26 17:03

AlexV