Skip to content Skip to sidebar Skip to footer

Android : How To Remove White Spaces In Chinese Characters?

I have a problem on trimming whitespaces in Chinese characters. I tried to log the content and here is how it looks like: When displaying it in textview, it does display Chinese c

Solution 1:

Chinese and Japanese don't use the regular space character ' '. The languages use their own that is the same width as the characters. This is the character here ' ', you should write a manual trim function to check for that character at the beginning and end of the string.

You may be able to directly use the character if you convert your code file to unicode (if java will allow). Otherwise you will need to find the unicode character code for ' ' and check if the character code is at the beginning or end of the string.

The following link tells us that the ideographic space is 0xe38080 in UTF-8 and 0x3000 in UTF-16, and that Java's Character.isSpaceChar() function will return true. I would have thought String.trim() would have used this property to determine whether or not to trim though.

http://www.fileformat.info/info/unicode/char/3000/index.htm

Solution 2:

Solution 3:

To trim whitespaces in unicode which is having 2 byte use string replace.

replace 2byte space with 1byte space. 0x3000 is the hexadecimal value of unicode IDEOGRAPHIC SPACE

String.replace("\u3000"," ").trim()

Post a Comment for "Android : How To Remove White Spaces In Chinese Characters?"