startsWith(string prefix) requires two strings and searches if both strings start with the same value, is there a way too modify startsWith(string prefix) to search one string for re-occuring characters?
Lets say
String str = "MANAMA";
I want to be able to search that string using startsWith() and endsWith() or some variation to determine whether or not the prefix or suffix of the string are the same. The only problem is those methods search two strings to see if they have matching suffix’s or prefix’s
startsWith(string prefix) does this :
String str1 = "MANAMA";
String str2 = "PANAMA.";
// Sets what characters to search for from above strings
String startStr = "MA";
// Searchs for startStr in strings
boolean starts1 = str1.startsWith(startStr);
boolean starts2 = str2.startsWith(startStr);
// Display the results of the startsWith calls.
System.out.println(starts1);
System.out.println(starts2);
endsWith(string suffix) does :
String str1 = "MANAMA";
String str2 = "PANAMA.";
// Sets what characters to search for from above strings
String endStr = "MA";
// Searchs for startStr in strings
boolean ends1 = str1.endsWith(endStr);
boolean ends2 = str2.endsWith(endStr);
// Display the results of the endsWith calls.
System.out.println(ends1);
System.out.println(ends2);
Is there a way to modify or a different method to be able to search WITHIN one string instead of searching two strings for endStr and startStr?
Not sure if I’m understanding your question correctly but you can use
endsWith()andstartsWith()on the same string. For example: