How to check if a variable is empty in tcl

How can you if the variable is empty? It can contain "\n"

either spaces. I am currently doing this

if {{string trimleft $var} != ""} {
   # the variable is not empty
   puts $var
 }

      

However, the printed variable still appears to be empty? will trimleft

delete "\n"

? Is there a better way to check if a string is empty?

+3


source to share


2 answers


if {{string trimleft "\n"} eq ""} {
    puts "1"
} elseif {"\n" eq ""} {
    puts "2"
}

      



  • The above script will output the output as nothing.
  • I think trimleft

    it will not remove \n

    from the value.
+2


source


As far as I know, checking for an empty string is simple:

if {$myString eq ""} {

    puts "string is empty"

}

      

Doing the following shouldn't print anything:



if {" " eq ""} {
    puts "1"
} elseif {"\n" eq ""} {
    puts "2"
}

      

I hope I understood your question correctly.

+4


source







All Articles