I have a string, and I want to check whether it begins with a certain character (# in this case). I'd like to know the most efficient way of doing this!
What I'm thinking of is if {[string compare -length 1 $string "#"]}
, because it might use strcmp() which will probably be one of the fastest ways to achieve this.
What I think might also be possible is if {[string index $string 1] == "#"}
, because it might do *string == '#' which will probably also be very fast.
What do you think?
string compare -length
does end up using the equivalent ofstrncmp()
, it has to be more careful than you might imagine because Tcl uses Unicode characters and not ASCII and NUL is a legal character in a Tcl string. – Devote