Java | split()
La méthode split() divise une chaîne donnée en fonction d’une expression régulière.
Syntaxe
public string[] split(String regex, int limit)
Paramètres
- regex(Obligatoire) : Une expression régulière.
- limit(Obligatoire) : Le seuil de résultat.
Valeur de retour
La fonction renvoie un tableau de chaînes calculé en divisant la chaîne donnée.
Exemple :
Le code suivant divise la chaîne « Java,PHP,C++,Python » en quatre partie, en fonction du séparateur:
public class Main { public static void main(String args[]) { String str = "Java,PHP,C++,Python"; String[] tab = str.split(",", 4); for (String s : tab) System.out.println(s); } }
Sortie:
Java PHP C++ Python