android拆分空间不适合这种情况.为什么?

Nar*_*ngh 12 java android split

我需要突出显示文本中的网址并动态点击.

为此,我使用以下方法

private SpannableString addClickablePart(String string) {


        string = string.replaceAll("\\n"," \n ");

        string += " ";
        SpannableString ss = new SpannableString(string);
        String[] words = string.split(" ");
        for (final String word : words) {


            if (CommonUtilities.isValidURL(word)) {


                int lastIndex = 0;

                while(lastIndex != -1){

                    lastIndex = string.indexOf(word+" ",lastIndex);

                    if(lastIndex != -1){
                        ClickableSpan clickableSpan = new ClickableSpan() {
                            @Override
                            public void onClick(View textView) {
                                //use word here to make a decision

                                isRefreshingNecessary = false;

                                Intent mIntent = new Intent(ctx, UserWebsiteActivity.class);
                                mIntent.putExtra("website", word);
                                startActivity(mIntent);
                            }
                        };

                        ss.setSpan(clickableSpan, lastIndex, lastIndex + word.length(),
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                        lastIndex += word.length();
                    }
                }


            }
        }

        return ss;
    }
Run Code Online (Sandbox Code Playgroud)

它适用于大多数情况.但是,不适用于所有情况,如下例所示.

在您的计划条款和条件中提供给您的有关这些编号范围的定价信息将不再适用,并将被此收费结构取代.有关详细信息,请访问www.ee.co.uk/ukcalling.

对于上述情况,当我使用分割整个字符串时

 String[] words = string.split(" ");
Run Code Online (Sandbox Code Playgroud)

要么

 String[] words = string.split("\\s+");
Run Code Online (Sandbox Code Playgroud)

我得到See www.ee.co.uk/ukcalling for了一个单词.相反,我需要这些3 - See,www.ee.co.uk/ukcallingfor为3个不同的话,不被作为归类为一个字.

我无法理解与太空分裂的方式有什么不对.请帮我知道.

小智 4

替换所有不可见的空白字符。

 string = string.replaceAll("\\t", " ");
 string = string.replaceAll("\\xA0", " ");
 string = string.replaceAll("\\u1680", " ");
 string = string.replaceAll("\\u180e", " ");
 string = string.replaceAll("\\u2000", " ");
 string = string.replaceAll("\\u200a", " ");
 string = string.replaceAll("\\u202f", " ");
 string = string.replaceAll("\\u205f", " ");
 string = string.replaceAll("\\u3000", " ");
Run Code Online (Sandbox Code Playgroud)

对于java 8

string = string.replaceAll("(^\\h*)|(\\h*$)","");
Run Code Online (Sandbox Code Playgroud)

检查这个how-to-trim-no-break-space-in-java