Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem trimming Japanese string in java

Tags:

java

string

nlp

I have the following string (japanese) " ユーザー名" , the first character is "like" whitespace but its number in unicode is 12288, so if I do " ユーザー名".trim() I get the same string (trim doesn't work). If i do trim in c++ it works ok. Does anyone know how to solve this issue in java? Is there a special trim method for unicode?

like image 898
Pablo Retyk Avatar asked Jan 26 '09 13:01

Pablo Retyk


2 Answers

As an alternative to the StringUtils class mentioned by Mike, you can also use a Unicode-aware regular expression, using only Java's own libraries:

" ユーザー名".replaceAll("\\p{Z}", "")

Or, to really only trim, and not remove whitespace inside the string:

" ユーザ ー名 ".replaceAll("(^\\p{Z}+|\\p{Z}+$)", "")
like image 128
Fabian Steeg Avatar answered Oct 28 '22 17:10

Fabian Steeg


Have a look at Unicode Normalization and the Normalizer class. The class is new in Java 6, but you'll find an equivalent version in the ICU4J library if you're on an earlier JRE.

    int character = 12288;
    char[] ch = Character.toChars(character);
    String input = new String(ch);
    String normalized = Normalizer.normalize(input, Normalizer.Form.NFKC);

    System.out.println("Hex value:\t" + Integer.toHexString(character));
    System.out.println("Trimmed length           :\t"
            + input.trim().length());
    System.out.println("Normalized trimmed length:\t"
            + normalized.trim().length());
like image 34
McDowell Avatar answered Oct 28 '22 17:10

McDowell