I wanted to implement a method which receives an array of String
as a parameter and returns a new array of the same type and size as the one passed as a parameter. The resulting array will contain the same words as the original, except those that end in the indicated word, which will be modified by removing that suffix
public static String[] palabras (String[] array) {
String search = "mente";
for (String i:array) {
if (i.equals(search)) { // Al principio lo tenía de la forma (i == search)
array = array.endswith(i, 0, array.length); // endwiths(valor, inicio, fin)
}
}
return array;
}
As I understand it, what you want is to remove the suffix on each of the words in the array.
When iterating through the array, you should check for the suffix (
endsWith()
) on each word, rather than on the array itself ( as far as I know , arrays have no methodendswith
). This must be the condition, instead of looking for the word to be equal to the suffixsince this will only be true when both words are exactly the same, and not when the word contains the suffix at the end.
What you could try is to evaluate the suffix and then modify your array. I present the following example.
I hope it helps you :)