2011-11-20 34 views

回答

2

您可以使用String類的indexOf方法:

String haystack = "the red cat is watching the red car stopped at the red stop sign"; 
String needle = "red"; 
int idx = 0, pos; 
while((pos = haystack.indexOf(needle,idx)) != -1) { 
     System.out.println(pos+1); 
     idx += pos+1;  
} 

See it

+0

感謝您幫助我找到丟失的針頭! ;-) – SliverNinja

相關問題