Liam Andersson :
I am developing a program that asks the user for a word / letter. In this example i will type 'a'. The program will then see if 'a' is a prefix,suffix or infix of the word. I quickly figured out how to figure out the prefix and suffix using "startsWith(String s).
However, I wonder how I will be able to see the infix of the String s. The infix in hat is 'a' but how can I make my program figure that out?
So my question is:
How will my program be able to tell if 'a' is an infix of "hat"?
Is there some sort of algorithm or method I should use? I am really stuck here.
I don't have any code due to the fact that I don't know how to face this problem.
Thanks in advance!
Andreas :
Use indexOf(String str)
:
String word = "hat";
String letter = "a";
int index = word.indexOf(letter);
if (index == -1) {
System.out.println("Not found");
} else if (index == 0) {
System.out.println("Prefix");
} else if (index == word.length() - letter.length()) {
System.out.println("Suffix");
} else {
System.out.println("Infix");
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=237560&siteId=1