2011-09-08 20 views
1

嗨,我正在使用特定关键字在应用程序中执行搜索操作,并列出结果列表视图活动。结果将在搜索关键字前后显示50个字符,但是当我用50个字符打破单词时,结果中的半字或单个字母显示在开头和结尾。我怎么能忽略并得到完整的单词android应用程序搜索操作与破碎的单词

以下是我的代码。请帮助我做到这一点。在此先感谢

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

假设一个空间分隔符字:

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); 
+0

非常感谢,这是又一个伟大的help..thanks – Jes

相关问题