2013-10-05 23 views
-4

在java中,我怎麼能找到第二個/第n出現的表達式的子字符串,直到它的第三個/ n + 1出現在字符串中。在Java中發現子串之間的出現

事情是這樣的:

生活;; ;;是美麗;;世界正處在列表

結果:

beautiful 
+1

downvoters請發表評論,讓OP知道在哪裏以及是什麼原因 – SpringLearner

+4

[你有什麼嘗試?](http://www.whathaveyoutried.com/)我的意思是*除了*問我們。 –

+0

嘗試遵循本指南,然後再問:http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist –

回答

1

只需使用subString(start,end)

防爆。

String s="India Is Great"; 
String s1=s.subString(1,4); 

現在,您可以在s1中檢查所需的字符串。

這將是NDI

+0

String s =「patriotism ;; sucks ;; hardly; String [] tokens = s.split(「;;); String result = tokens [2]; – Gab

+0

這只是我通常使用的樣本字符串。 @Gab –

0

我不知道你正在試圖獲得,但如果你知道這個列表是由兩個分號(;;),然後分割字符串,並從獲得的值你得到一個數組

4

你可以使用split()來實現它。以下是打印所需輸出的代碼。

public static void main(String[] args) { 
    String str = "life;;is;;beautiful;;world"; 
    int n=2; 
    String[] strArray = str.split(";;"); 
    System.out.println(strArray[n]); 
} 

希望這會解決你的目的。

+0

這是更好的.. :) ..雖然它可以拋出indexoutofbound異常.. – Anirudha