Android:如何删除汉字中的空格? [英] Android : How to remove white spaces in Chinese characters?

查看:446
本文介绍了Android:如何删除汉字中的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在修剪汉字的空格时有问题。我尝试记录内容,下面是它的样子:

I have a problem on trimming whitespaces in Chinese characters. I tried to log the content and here is how it looks like:

在textview中显示它时,它显示汉字,但问题是字符串前后的空格。有人可以帮我编码/解码这个?提前感谢。

When displaying it in textview, it does display Chinese characters but the problem is the whitespace before and after the string text. Can someone help me to encode/decode this? thanks in advance.

编辑1:添加了结果屏幕截图。

EDIT 1 : Added screenshot of result.

strong> EDIT 2:添加了内容字符集作为响应。

EDIT 2 : Added content charset in response.

HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

但我仍然得到正方形字符,当记录时,当以XML布局显示时,方形字符变为空白。

but I still get the square characters when logging and when displaying in XML layout, the square characters become whitespaces.

编辑3:添加了我的工作解决方案。

EDIT 3 : Added my working solution.

    private String removeWhiteSpace(String oldString) {
    String newString = null;
    if (oldString.length() > 0) {
        Character c = oldString.charAt(0);

        boolean isWhiteSpace = Character.isWhitespace(c);                           
        if (isWhiteSpace) {
            newString = oldString.replace(c, ' ');
        } else {
            newString = oldString;
        }

        newString = newString.trim();
    }
    return newString;
}


推荐答案

中文和日文使用常规空格字符。这些语言使用自己的,与字符相同的宽度。这是这里的字符,你应该写一个手动修剪函数来检查字符串的开头和结尾处的那个字符。

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.

你可以直接如果您将代码文件转换为unicode(如果java将允许)使用字符。否则,您将需要找到''的unicode字符代码,并检查字符代码是否在字符串的开头或结尾。

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.

以下链接告诉我们,表意空间在UTF-8中为0xe38080,在UTF-16中为0x3000,并且Java的Character.isSpaceChar()函数将返回true。我会认为String.trim()将使用这个属性来确定是否修剪。

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

这篇关于Android:如何删除汉字中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆