Search code examples
argumentstcl

What is the purpose of `last` argument in TCL 8.4 string totitle command?


TCL 8.4 doc defines the command : string totitle string ?first? ?last? When is the ?last? argument ever relevant?

I have tried the command on multi-word strings assuming that it will auto-capitalize characters after a space character if its in range of the [first, last] indices, but it does not. I don't see a practical reason why this argument exists otherwise.


Solution

  • First, your understanding of this command is incorrect. Only the first character will be capitalized, all others will be lowercase.

    However, let say you have a string that is has a proper name in it (eg. "cHAPter one: Frank's first flight"), and you do not want the person's name to be made lowercase. In my contrived example below, I know there is a colon before the person's name, and I can use that to stop 'totitle' from changing characters past the colon.

    Example:

    set myString "cHAPter one: Frank's first flight"
        
    set index [string first \: $myString]
        
    puts [string totitle $myString 0 end]
        
    # results in: Chapter one: frank's first flight
    # Whereas:
    
    puts [string totitle $myString 0 $index]
        
    # results in: Chapter one: Frank's first flight