Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,9 @@
|
|||
int countSubstring(String string, String substring) {
|
||||
substring = Pattern.quote(substring);
|
||||
Pattern pattern = Pattern.compile(substring);
|
||||
Matcher matcher = pattern.matcher(string);
|
||||
int count = 0;
|
||||
while (matcher.find())
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
public class CountSubstring {
|
||||
public static int countSubstring(String subStr, String str){
|
||||
return (str.length() - str.replace(subStr, "").length()) / subStr.length();
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
System.out.println(countSubstring("th", "the three truths"));
|
||||
System.out.println(countSubstring("abab", "ababababab"));
|
||||
System.out.println(countSubstring("a*b", "abaabba*bbaba*bbab"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
public class CountSubstring {
|
||||
public static int countSubstring(String subStr, String str){
|
||||
// the result of split() will contain one more element than the delimiter
|
||||
// the "-1" second argument makes it not discard trailing empty strings
|
||||
return str.split(Pattern.quote(subStr), -1).length - 1;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
System.out.println(countSubstring("th", "the three truths"));
|
||||
System.out.println(countSubstring("abab", "ababababab"));
|
||||
System.out.println(countSubstring("a*b", "abaabba*bbaba*bbab"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
public class CountSubstring {
|
||||
public static int countSubstring(String subStr, String str){
|
||||
int count = 0;
|
||||
for (int loc = str.indexOf(subStr); loc != -1;
|
||||
loc = str.indexOf(subStr, loc + subStr.length()))
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
System.out.println(countSubstring("th", "the three truths"));
|
||||
System.out.println(countSubstring("abab", "ababababab"));
|
||||
System.out.println(countSubstring("a*b", "abaabba*bbaba*bbab"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue