Created
February 21, 2024 07:57
-
-
Save swapnilshrikhande/264bc7687f9aab9308625ca9e9530b80 to your computer and use it in GitHub Desktop.
Trim Non Space Characters In java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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