Reputation: 11651
How do you ceck if a variable is empty ? It could contain a "\n"
or spaces.
I am currently doing this
if {{string trimleft $var} != ""} {
# the variable is not empty
puts $var
}
However the variable printed still seems to be empty ? will trimleft
remove "\n"
?
Is there a better approach to check if a string is empty ?
Upvotes: 11
Views: 58139
Reputation: 111
To answer the original question with a concern of checking for an empty line here is the corrected code example:
if {[string trim $var] != ""} {
puts $var
}
Without specifying the ?chars? the trim will remove white space chars which are considered to be space, tab, newline and carriage return.
Upvotes: 5
Reputation: 42
if {{string trimleft "\n"} eq ""} {
puts "1"
} elseif {"\n" eq ""} {
puts "2"
}
trimleft
won't remove \n
from the value.Upvotes: 1
Reputation: 141
As far as I know, checking if a string is empty is simply done by:
if {$myString eq ""} {
puts "string is empty"
}
Running the following should not print anything:
if {" " eq ""} {
puts "1"
} elseif {"\n" eq ""} {
puts "2"
}
I hope I understood your question correctly
Upvotes: 10