संभव डुप्लिकेट शामिल हैं:
Occurences of substring in a stringकितनी बार एक स्ट्रिंग एक और
जांच करने का तरीका कितनी बार एक स्ट्रिंग एक और एक में शामिल है विषय के रूप में? उदाहरण:
s1 "babab"
s2 "bab"
Result : 2
तो मैं Matcher यह केवल पहला मामला पहचान करता है का उपयोग करें:
String s1 = JOptionPane.showInputDialog(" ");
String s2 = JOptionPane.showInputDialog(" ");
Pattern p = Pattern.compile(s2);
Matcher m = p.matcher(s1);
int counter = 0;
while(m.find()){
System.out.println(m.group());
counter++;
}
System.out.println(counter);
मुझे लगता है कि जैसे यह कर सकते हैं, लेकिन मैं उपयोग करने के लिए जावा लाइब्रेरीज iike स्कैनर, StringTokenizer नीचे चाहते हैं, Matcher आदि:
String s1 = JOptionPane.showInputDialog(" ");
String s2 = JOptionPane.showInputDialog(" ");
String pom;
int count = 0;
for(int i = 0 ; i< s1.length() ; i++){
if(s1.charAt(i) == s2.charAt(0)){
if(i + s2.length() <= s1.length()){
pom = s1.substring(i,i+s2.length());
if(pom.equals(s2)){
count++;
}
}
}
}
System.out.println(count);
क्या यह एक होमवर्क असाइनमेंट है? –
अंतिम मिली इंडेक्स से शुरू होने पर आप बस थोड़ी देर में 'स्ट्रिंग # इंडेक्सऑफ()' का उपयोग कर सकते हैं। – assylias
http://stackoverflow.com/questions/767759/occurences-of-substring-in-a-string –