gpt4 book ai didi

java - android splitting with space 不适用于这种情况。为什么?

转载 作者:搜寻专家 更新时间:2023-10-30 21:10:07 25 4
gpt4 key购买 nike

我需要突出显示并使文本中的 url 可动态点击。

为此,我使用以下方法

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;
}

它适用于大多数情况。但是,并非适用于所有情况,如下例所示。

The pricing information provided to you in your plan terms and conditions about these number ranges will no longer apply and will be replaced by this charging structure. See www.ee.co.uk/ukcalling for further information.

对于上述情况,当我使用

拆分整个字符串时
 String[] words = string.split(" ");

 String[] words = string.split("\\s+");

我得到了 See www.ee.co.uk/ukcalling for 作为一个词。相反,我需要这 3 个 - Seewww.ee.co.uk/ukcallingfor 作为 3 个不同的词,而不是组合成一个词。

我无法理解空间 split 的方式有什么问题。请帮助我知道。

最佳答案

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

 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", " ");

Java 8

string = string.replaceAll("(^\\h*)|(\\h*$)","");

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

关于java - android splitting with space 不适用于这种情况。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com