What is the most efficient way to check if a string starts with a specific character in TCL?

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.

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.

What do you think?

+5
source share
2 answers

The fastest way to check if a string starts with a specific character is string match:

if {[string match "#*" $string]} { ... }

, , , $string. - ( , , string match -).

+12

[string match "*" $string_name] , . regexp .

0

All Articles