您好我正在使用特定关键字在应用中执行搜索操作,并将结果列在列表视图活动中。结果将在搜索关键字之前和之后显示50个字符,但是当我取50个字符时,它会中断单词,结果在开头和结尾显示半个单词或单个字母。我怎么能忽略并得到完整的单词
以下是我的代码。请帮我这个。提前谢谢
int endindex = searchTextStartIndex + searchTextLength + 50;
if (searchTextStartIndex > 50) {
startindex = searchTextStartIndex - 50;
}
if (endindex > datalength) {
endindex = datalength;
}
searchdata = searchdata.substring(startindex, endindex);
} catch (Exception e)
答案 0 :(得分:0)
假设空格为单词分隔符:
if (searchTextStartIndex > 50) {
startindex = searchdata.lastIndexOf( " ", (searchTextStartIndex - 50) );
}
if (startindex == -1) startindex = 0;
int endindex = searchdata.indexOf( " ", (searchTextStartIndex + searchTextLength + 50) );
if (endindex == -1 || endindex > datalength) {
endindex = datalength;
}
searchdata = searchdata.substring(startindex, endindex);