मैं जावा में एक स्ट्रिंग में एक सबस्ट्रिंग की सभी घटनाओं को खोजने की कोशिश कर रहा हूं।जावा 0 स्ट्रिंग में स्ट्रिंग में सबस्ट्रिंग की सभी घटनाएं खोजें
उदाहरण के लिए: खोज "ababsdfasdfhelloasdf" "asdf" के लिए वापस आएगा [8,17] के बाद से वहाँ 2 "asdf" की, स्थिति 8 में एक और एक 17. पर "आ के लिए" aaaaaa "सर्च कर रहे हैं कर रहे हैं "वापसी होगी [0,1,2,3,4] क्योंकि वहाँ एक" पदों 0,1,2,3 पर आ ", और 4.
मैं इस कोशिश की:
public List<Integer> findSubstrings(String inwords, String inword) {
String copyOfWords = inwords;
List<Integer> indicesOfWord = new ArrayList<Integer>();
int currentStartIndex = niwords.indexOf(inword);
int indexat = 0;
System.out.println(currentStartIndex);
while (cthing1 > 0) {
indicesOfWord.add(currentStartIndex+indexat);
System.out.println(currentStartIndex);
System.out.println(indicesOfWord);
indexat += cthing1;
copyOfWords = copyOfWords.substring(cthing1);
System.out.println(copyOfWords);
cthing1 = copyOfWords.indexOf(inword);
}
यह पाइथन में समस्या को हल किया जा सकता है:
indices = [m.start() for m in re.finditer(word, a.lower())]
जहां "शब्द" वह शब्द है जिसे मैं ढूंढ रहा हूं और "ए" वह स्ट्रिंग है जिसे मैं खोज रहा हूं।
मैं जावा में इसे कैसे प्राप्त कर सकता हूं?
मुझे लगता है कि शीर्ष पद [यहां] (http://stackoverflow.com/questions/767759/occurrences-of-substring-in-a-string) आपकी मदद कर सकता है। इंडेक्स प्राप्त करने के लिए, 'अंतिम इंडेक्स' को प्रिंट या सहेजने के बाद ही उन्हें प्राप्त करें। –
क्या आपका मतलब है कि आपको [इस तरह कुछ] चाहिए (http://ideone.com/9IeCEQ)? –
कृपया अधिक अर्थपूर्ण चर नामों का उपयोग करें। यह समझना मुश्किल है कि 'cthing1' या' outthing' या 'niwords' का अर्थ क्या है। 'LastIndex', 'indexList', आदि जैसी चीजों का उपयोग करना, यह समझना आसान होगा कि आपने जो लिखा और उसे सही किया। – RealSkeptic