Action disabled: export_raw

Tcl string concatenation

This page is part of the Tcl/Tk page of the Airplug documentation.
Back to the Tcl/Tk page / Back to Airplug documentation page.

  • Two string variables can easily be concatenated as follows:
    set alpha "Hel"
    set beta "lo"
    puts stdout $alpha$beta

    This gives:

    Hello
  • Another way is to use append:
    set alphabeta "Hel"
    append alphabeta "lo"
    puts stdout $alpha$beta

    This gives:

    Hello
  • However, when mixing a string variable and a literal string, the result is not what would be expected:
    set alpha "Hel"
    puts stdout $alpha"Hel"

    This gives:

    Hel"lo"
  • It is important to note that the concat tcl function applies on lists. It is possible to concat some strings by using a list but this is not the good way:
    set alphabeta [concat "Hel" "lo"]
    puts stdout $alphabeta

    This gives a list of two members, displayed with a space between them:

    Hel lo

    A list can be merged to give a string (here without spaces between members) as follows:

    set alphabeta [join [concat "Hel" "lo"] ""
    puts stdout $alphabeta

    This gives:

    Hello

    However this is not efficient.

  • Finally, the simplest way is by using curly braces to separate the variable name:
    set alpha "Hel"
    set alphabeta ${alpha}lo
    puts stdout $alphabeta

    This gives:

    Hello
  • Conclusion:
    • With append:
      set alphabeta "Hel"
      append alphabeta "lo"
    • With curly braces:
      set alpha "Hel"
      set alphabeta ${alpha}lo

<html><br></html>


This page is part of the Tcl/Tk page of the Airplug documentation.
Back to the Tcl/Tk page / Back to Airplug documentation page.

 
Back to top
en/doc/pub/tcltk/tips/concat.txt · Last modified: by 127.0.0.1
 
 
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0