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.
Feed/

How can you compute a substring of a string?

No cover

Channel

Computer science one

1 page

"There are two methods that allow you to compute a substring of a string. The first allows you to specify a beginning index with the entire remainder of the string being included in the returned string. The second allows you to specify a beginning index as well as an ending index. In both cases, the beginning index is inclusive (that is the resulting string includes the character at that index), but in the second, the ending index is exclusive (the character at the end index is not included).\n\nString name = \"Thomas Alan Waits\";\n\nString firstName = name.substring(0, 6); //\"Thomas\"\n\nString middleName = name.substring(7, 11); //\"Alan\"\n\nString lastName = name.substring(12); //\"Waits\"\n\nThe result of the two argument substring() method will always have length equal to endIndex - beginIndex."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.