2016-11-09 88 views
1

我想確定我的索引是否在單詞邊界處。識別字符串中單詞的第一個字符

E.g.在字符串"National Aeronautics Space Research is an amazing organization"

我想檢查我的指數是在researchR。我可以使用一個charAt(index),但我想使這個通用,因爲這將是一個更大的算法識別首字母縮寫詞的一部分。所以基本上我想確定詞的邊界。

幫助這將不勝感激。

+2

如果index大於0,那麼你可以檢查index-1的值?識別單詞界限*?你能解釋一下你想要做什麼嗎? – TheLostMind

+2

請注意,這不是一個免費的代碼 - wri我們非常樂意幫助其他程序員(和有志之士)編寫他們自己的代碼。請閱讀[如何提出問題](http://stackoverflow.com/help/how-to-ask)上的幫助主題。之後,請用您迄今編寫的代碼更新您的問題,以完成您希望實現的任務。 –

回答

0

我沒有得到你的你的問題的第二部分,「但我想使這個普通的....,

public class IndexVal { 
     public static void main(String args[]){ 

      String goodText="National Aeronautics Space Research is an amazing organization"; 
      String[] parts = goodText.split(" "); 
      for (String part : parts) { 
       System.out.println("Index is at a word boundary " +part.charAt(0)+" "); 

      } 
     } 
    } 
0

據我瞭解,你想獲得字邊界的索引。然後,它使用正則表達式

Pattern pattern = Pattern.compile("\\s\\w"); 
    String s = "National Aeronautics Space Research is an amazing organization"; 
    Matcher matcher = pattern.matcher(s); 
    while (matcher.find()) { 
     System.out.println(matcher.start()); 
    } 
相關問題