String in TCL
We know that variable value can be in the form of a number or string. Working with string in tcl, we can have many ways to take care of things.
set a 10;
set a apple;
With the above example we can see that 'a' can have the value 10 or apple. There are two ways we can set the variable.
set string “This is a string”
set string {This is a string}
String Escape Sequence:
We do have string escape sequence which helps us to get results in better ways.
#1. \n (newline) -> This is known as new line character. Using this we break the sentence and new lines will be printed after this character.
Eg. puts “This is a \n string”
Result: This is a
string
#2. \t (Tab) -> This is known as tab and using this we can create a tab space between the strings.
Eg. puts “This is a \t string”
Result: This is a string
#3. \v (Vertical tab) -> This is vertical tab, using this we can have a gap of vertical tab in between strings.
Eg. puts “This is a \v string”
Result: This is a
string
#4. \b (space) -> This is backspace and used for creating a space.
Eg. puts “This is a \b string”
Result: This is a string
If you want to add any special character like \, “, ‘, ? etc then use backslash (\) before these. Below are the examples.
\\ -> This will result into a backslash when used with a string.
\’ -> This will result into a ‘ when used with a string.
\” -> This will result into a “ when used with a string.
\? -> This will result into a ? when used with a string.
Eg.
puts “This is a \’ string \’ ”
puts “Is this a string\? ”
String Commands
# Compare string:
If one need to compare the strings, then they can use below command.
string compare string1 string2
Eg.
% string compare error fatal
-1
% string compare error error
0
% string compare fatal error
1
% string compare abc def
-1
% string compare def abc
1
We can see that 1 and -1 is getting decided based on alphabetic, if a string starts with the word which comes first in alphabetic, then this will return -1 otherwise if reverse then it will return 1.
# First string1 string2
This command shows up the first occurrence of the string1 from string2.
% string first r error
1
% string first e error
0
% string first o error
3
# Last string1 string2
This command shows up the last occurrence of the string1 from string2.
% string last r error
4
% string last e error
0
% string last a error
-1
% string last o error
3
# Index string index:
This command will point to the index.
% string index error 1
r
% string index error 3
o
# string length string:
This command gives the length of string.
% string length error
5
# Range:
This command gives the range of string.
% string range error 2 4
ror
# Tolower string:
This command converts the string into lower characters.
% string tolower ERror
error
#Toupper String:
This command converts the string into upper characters.
% string toupper ERror
ERROR
# trimright:
This command trim the string character from right.
% string trimright error ror
e
# trimleft:
This command trim the string character from left.
% string trimleft error er
or
#trim command:
This command trim the characters.
% string trim error er
o
% string trim error e
rror
% string trim error r
erro
Format Command
%d -> Integer representation
%s -> String representation
%f -> Floating representation
%O -> Octal Representation
%x -> Hexadecimal Representation
% puts [format “%d” 2]
2
% puts [format “%f” 2.5]
2.50000
% puts [format “%s” string]
string
%puts [format “%x” 20]
14
% puts "[format "%x" 10]"
a
% puts "[format "%o" 10]"
12