io.magentys.cinnamon.webdriver.elements.StringNormaliser Maven / Gradle / Ivy
package io.magentys.cinnamon.webdriver.elements;
public final class StringNormaliser {
// Suppresses default constructor, ensuring non-instantiability.
private StringNormaliser() {
}
/**
* Normalises text as follows:
*
*
* - trims leading and trailing whitespace
* - replaces any kind of hyphen or dash character with a dash from a default character set
* - replaces all carriage return characters with a single space
* - replaces all new line characters with a single space
* - replaces all multiple whitespace with a single space
* - removes all non-ASCII characters
*
*
* @param text the text to be normalised
* @return Normalised text
*/
public static String normalise(String text) {
return text.trim().replaceAll("\\p{Pd}", "-").replaceAll("\\r", " ").replaceAll("\\n", " ").replaceAll("\\s+", " ")
.replaceAll("[^\\x20-\\x7e]", "");
}
}