TCL Strings
TCL Strings
Strings
Basem Atia
June-2025
TCL Strings
String first
➢ Command Description
➢ string first string1 string2
➢ Returns the index of the character in string1 that starts the first match to string2, or -1 if there is no match
➢ Example:
puts [string first "l" "hello"]
puts [string first "z" "hello"]
➢ Output:
2
-1
➢ Example:
puts [string last "l" "hello"]
puts [string last "z" "hello"]
➢ Output:
3
-1
➢ Example:
puts [string equal "hello" "hello"]
puts [string equal "hello" "Hello"]
➢ Output:
1
0
➢ Example:
puts [string match "he*" "hello"]
puts [string match "he?" "hello"]
➢ Output:
1
0
➢ Example:
puts [string range "abcdef" 2 4]
➢ Output:
cde
➢ Example:
puts [string index "hello" 1]
➢ Output:
e
➢ Example:
puts [string length "hello"]
➢ Output:
5
➢ Example:
puts [join {apple banana cherry} ", "]
➢ Output:
apple, banana, cherry
➢ Example:
set str "a,b,c"
set result [split $str ","]
puts $result
➢ Output:
abc
➢ Example:
set a "Hello"
append a " World"
puts $a
➢ Output:
Hello World
➢ Example:
puts [regexp {h.llo} "hello"]
puts [regexp {\d+} "abc123"]
➢ Output:
1
1
➢ Example:
set str "abc123"
regsub {\d+} $str "NUM" newstr
puts $newstr
➢ Output:
abcNUM
➢ Example:
puts [format "Pi is approximately %.2f" 3.14159]
➢ Output:
Pi is approximately 3.14