Bifalgorithm post · Computer science one · Arrays of Strings
How can you compute a substring of a string?
How can you compute a substring of a string? (Computer science one · Arrays of Strings) — "There are two methods that allow you to compute a substring of a str…
Ideas in this flowchart
- La méthode substring() permet de calculer une sous-chaîne d’une chaîne.
- La première méthode reçoit un index de début et inclut tout le reste de la chaîne.
- La syntaxe substring(beginIndex) renvoie la sous-chaîne allant de beginIndex jusqu’à la fin.
- La seconde méthode reçoit un index de début et un index de fin.
- Règles concernant les index
- L’index de début est inclusif : le caractère situé à cet index est inclus.
- Avec deux arguments, l’index de fin est exclusif : le caractère situé à cet index n’est pas inclus.
- Exemples avec la chaîne « Thomas Alan Waits »
- name.substring(0, 6) produit « Thomas ».
- name.substring(7, 11) produit « Alan ».
- name.substring(12) produit « Waits ».
- La longueur du résultat à deux arguments est toujours endIndex - beginIndex.