简体   繁体   English

如何避免包含非标准字符的分词?

[英]How to avoid word break containing non-standard character?

In my application, a user enters a text. 在我的应用程序中,用户输入文本。 I put the text in the array and replace one of the letters of each word with a Japanese symbol in random order. 我将文本放入数组中,并以随机顺序用日语符号替换每个单词的字母之一。

word.replace(letter, japaneseSymbol);

For example: 例如:

WoあdOne wいrdTwo wordうhree wえrdFour wordFおve. Woあd一个单词い两个单词う三个单词四个单词おve。

Then I display the text in TextView: 然后在TextView中显示文本:

content.setText(TextUtils.join(" ", convertedWords));

The problem is that the line-break is not displayed correctly: 问题是换行符显示不正确:

WoあdOne wいrdTwo wordうhree w Woあd一个词い两个词うhree w
えrdFour wordFおve. “四字五”。

I do not want to break the word. 我不想毁了这个词。 Do you have any idea how to avoid a word break? 您知道如何避免单词打断吗?

I found a solution here - https://gamedev.stackexchange.com/questions/50547/how-to-force-a-line-break-before-the-last-word-of-a-line-reach-the-edge 我在这里找到了解决方案- https://gamedev.stackexchange.com/questions/50547/how-to-force-a-line-break-before-the-last-word-of-a-line-reach-the-边缘

public static String wrapString(String string, int charWrap) {
    int lastBreak = 0;
    int nextBreak = charWrap;
    if (string.length() > charWrap) {
        String setString = "";
        do {
            while (string.charAt(nextBreak) != ' ' && nextBreak > lastBreak) {
                nextBreak--;
            }
            if (nextBreak == lastBreak) {
                nextBreak = lastBreak + charWrap;
            }
            setString += string.substring(lastBreak, nextBreak).trim() + "\n";
            lastBreak = nextBreak;
            nextBreak += charWrap;

        } while (nextBreak < string.length());
        setString += string.substring(lastBreak).trim();
        return setString;
    } else {
        return string;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM