In questo nuovo articolo impareremo l’uso di altri tre sottocomandi del comando string:

  • length
  • index
  • range

Sottocomando length

# sintassi
string length str

Ritorna la lunghezza della stringa str.

set myStr "Remember: words have power!"
puts "My string has [string length $myStr] characters."
---
My string has 27 characters.

Sottocomando index

# sintassi
string index str i

Ritorna una stringa composta dal solo carattere che occupa la posizione i nella stringa str.

set myStr "Remember: words have power!"
set i 8
puts "The character \"[string index $myStr $i]\" is located at position number #$i."
---
The character ":" is located at position number #8.

Sottocomando range

# sintassi
string range str first last

Ritorna la porzione di stringa che parte dalla posizione first e termina alla posizione last della stringa str.

set myStr "Remember: words have power!"
puts "I love the word \"[string range $myStr 10 14]\"."
---
I love the word "words".

Nel prossimo articolo tratteremo la comparazione tra stringhe.