I have a string and I want to check if it starts with a specific character (# in this case). I would like to know the most effective way to do this!
I think this if {[string compare -length 1 $string "#"]}is because it can use strcmp (), which will probably be one of the fastest ways to achieve this.
if {[string compare -length 1 $string "#"]}
I think it is also possible to be if {[string index $string 1] == "#"}, because * string == '#' can do this, which will probably also be very fast.
if {[string index $string 1] == "#"}
What do you think?
The fastest way to check if a string starts with a specific character is string match:
string match
if {[string match "#*" $string]} { ... }
, , , $string. - ( , , string match -).
$string
[string match "*" $string_name] , . regexp .