mardi 25 avril 2017

Find the first character in a string which isn't a specified character

I can't get my head around this. Taking a basic course and this task is about finding the index of a character in a string which isn't one of a few pre-specified characters.

The textbook says the code should be:

public static int sokForstaIckeBland (String s, String tecken, int start) {
for (int i=start; i<s.length(); i++)
  if (tecken.indexOf(s.charAt(i)) == -1)
    return i;
return -1;
}

String s is the text where i wanna search for a character, string tecken is the characters i wanna exclude in the search and int start is where to start the search.

I don't understand why the if statement ends with -1, what does that do?

Aucun commentaire:

Enregistrer un commentaire