Méthode startsWith() en Java
La méthode startsWith() est utilisée pour vérifier si une chaîne commence par le préfixe spécifié.
Syntaxe
public boolean startsWith(String prefix, int toffset) 或 public boolean startsWith(String prefix)
Parameters
prefix-- Préfixe.
toffset-- La position dans la chaîne pour commencer la recherche.
Valeur de retour
Renvoie true si la chaîne commence par le préfixe spécifié ; sinon renvoie false.
Exemple
public class Test {
public static void main(String args[]) {
String Str = new String("www.example.com");
System.out.print("Valeur de retour :" );
System.out.println(Str.startsWith("www") );
System.out.print("Valeur de retour :" );
System.out.println(Str.startsWith("example") );
System.out.print("Valeur de retour :" );
System.out.println(Str.startsWith("exemple", 4) );
}
}
Le programme ci-dessus produit le résultat suivant :
返回值 :true 返回值 :false 返回值 :true
Autres extensions
Classe String de Java