2015-09-25 3 views
6

मैं जावा में एक स्ट्रिंग में एक सबस्ट्रिंग की सभी घटनाओं को खोजने की कोशिश कर रहा हूं।जावा 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())] 

जहां "शब्द" वह शब्द है जिसे मैं ढूंढ रहा हूं और "ए" वह स्ट्रिंग है जिसे मैं खोज रहा हूं।

मैं जावा में इसे कैसे प्राप्त कर सकता हूं?

+0

मुझे लगता है कि शीर्ष पद [यहां] (http://stackoverflow.com/questions/767759/occurrences-of-substring-in-a-string) आपकी मदद कर सकता है। इंडेक्स प्राप्त करने के लिए, 'अंतिम इंडेक्स' को प्रिंट या सहेजने के बाद ही उन्हें प्राप्त करें। –

+2

क्या आपका मतलब है कि आपको [इस तरह कुछ] चाहिए (http://ideone.com/9IeCEQ)? –

+1

कृपया अधिक अर्थपूर्ण चर नामों का उपयोग करें। यह समझना मुश्किल है कि 'cthing1' या' outthing' या 'niwords' का अर्थ क्या है। 'LastIndex', 'indexList', आदि जैसी चीजों का उपयोग करना, यह समझना आसान होगा कि आपने जो लिखा और उसे सही किया। – RealSkeptic

उत्तर

5

आप सभी ओवरलैपिंग मैचों को प्राप्त करने के लिए सकारात्मक रूप से कैप्चरिंग का उपयोग कर सकते हैं और कब्जे वाले सबस्ट्रिंग के सूचकांक प्राप्त करने के लिए Matcher#start का उपयोग कर सकते हैं।

the regex का सवाल है, ऐसा लगता है जैसे

(?=(aa)) 

जावा कोड में दिखेगा:

String s = "aaaaaa"; 
Matcher m = Pattern.compile("(?=(aa))").matcher(s); 
List<Integer> pos = new ArrayList<Integer>(); 
while (m.find()) 
{ 
    pos.add(m.start()); 
} 
System.out.println(pos); 

परिणाम:

[0, 1, 2, 3, 4] 

देखें IDEONE demo

0

एक रेगेक्स का उपयोग करना निश्चित रूप से सबस्ट्रिंग खोजने के लिए एक अत्यधिक भारी समाधान है, और यदि आपके सबस्ट्रिंग में . जैसे विशेष रेगेक्स वर्ण होते हैं तो यह विशेष रूप से एक समस्या होगी। यहाँ एक समाधान this answer से अनुकूलित है:

String str = "helloslkhellodjladfjhello"; 
String findStr = "hello"; 
int lastIndex = 0; 
List<Integer> result = new ArrayList<Integer>(); 

while(lastIndex != -1) { 

    lastIndex = str.indexOf(findStr,lastIndex); 

    if(lastIndex != -1){ 
     result.add(lastIndex); 
     lastIndex += 1; 
    } 
} 
+0

यह पोस्टर चाहता था के रूप में [aa "नहीं [0,1,2,4,4] के लिए [0,2,4] देता है। सभी उप-मैचों को खोजने के लिए findStr की केवल 1 लंबाई तक अंतिम इंडेक्स को बढ़ाने की आवश्यकता नहीं है। – JasonM1

+0

आप सही हैं, ओवरलैपिंग भाग भूल गए हैं। संपादन। –

संबंधित मुद्दे