-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstringPractice.java
More file actions
52 lines (50 loc) · 1.94 KB
/
Copy pathsubstringPractice.java
File metadata and controls
52 lines (50 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package StringsClass1;
/**
* Practicing methods of finding strings in strings (substrings).
*
* @author Allana Bailey
* @version 1
*/
public class substringPractice {
//does string a occur two or more times in string b?
public boolean twoOccurrences(String stringa, String stringb) {
int count = 0;
int fromIndex = 0;
while((fromIndex = stringb.indexOf(stringa, fromIndex)) != -1) {
count ++;
fromIndex++;
}
if(count >= 2) {
return true;
}
return false;
}
public void testing() {
String stringa1 = "by";
String stringb1 = "A story by Abby Long";
String stringa2 = "a";
String stringb2 = "banana";
String stringa3 = "atg";
String stringb3 = "ctgtatgta";
twoOccurrences(stringa1, stringb1);
System.out.println("Does " + stringa1 + " appear in " + stringb1 + " at least twice?");
System.out.println(twoOccurrences(stringa1, stringb1));
twoOccurrences(stringa2, stringb2);
System.out.println("Does " + stringa2 + " appear in " + stringb2 + " at least twice?");
System.out.println(twoOccurrences(stringa2, stringb2));
twoOccurrences(stringa3, stringb3);
System.out.println("Does " + stringa3 + " appear in " + stringb3 + " at least twice?");
System.out.println(twoOccurrences(stringa3, stringb3));
}
//print the part of the string following a given substring.
public String lastPart(String stringa, String stringb) {
int index = stringb.indexOf(stringa);
if(index == -1) {
System.out.println(stringa + " does not appear in " + stringb);
return stringb;
} else {
System.out.println("The part of the string after " + stringa + " in " + stringb + " is " + stringb.substring(index + stringa.length()));
return stringb.substring(index + stringa.length());
}
}
}