Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Created February 21, 2024 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swapnilshrikhande/264bc7687f9aab9308625ca9e9530b80 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/264bc7687f9aab9308625ca9e9530b80 to your computer and use it in GitHub Desktop.
Trim Non Space Characters In java
// Trim all whitespace characters
// It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
// Reference : https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isWhitespace-char-
public static String trim(String value){
// first replace characters which are ignored by java to space.
char spaceChar = ' ';
return value == null ? null
: value.replace('\u00A0',spaceChar)
.replace('\u2007',spaceChar)
.replace('\u202F',spaceChar)
.trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment