String structure
- Compare Two Strings:
string compare: Compares two strings and returns:0if the strings are equal.-1if the first string is less than the second string.1if the first string is greater than the second string.
puts [string compare "Light" "Light"] ;# Output: 0
puts [string compare "Golden" "Light"] ;# Output: -1
puts [string compare "Light" "Golden"] ;# Output: 1
- String Index:
string index: Returns the character at the specified index.
puts [string index "Hallo world" 3] ;# Output: l
puts [string index "Hallo world" 0] ;# Output: H
puts [string index "Hallo world" 10] ;# Output: d
- String Length:
string length: Returns the length of the string.
puts [string length "Hallo world"] ;# Output: 11
- String Range:
string range: Returns a substring from the specified range.
puts [string range "Hallo world" 2 9] ;# Output: llo worl
puts [string range "I am studying math" 2 12] ;# Output: am studying
- String to Lowercase:
string tolower: Converts the string to lowercase.
puts [string tolower "HALLO WORLD"] ;# Output: hallo world
- String to Uppercase:
string toupper: Converts the string to uppercase.
puts [string toupper "hallo world"] ;# Output: HALLO WORLD
- String Trim Right:
string trimright: Trims characters from the right side of the string.
set string1 " chip world is vlsi academy "
set string2 " is vlsi academy "
puts [string trimright $string2 $string1] ;# Output: chip world
- String Trim Left:
string trimleft: Trims characters from the left side of the string.
set string1 " chip world is vlsi academy "
set string2 " chip world is "
puts [string trimleft $string1 $string2] ;# Output: vlsi academy
- String Trim:
string trim: Trims characters from both sides of the string.
set string1 ": chip world is vlsi academy :"
set string2 " : "
puts [string trim $string1 $string2] ;# Output: chip world is vlsi academy
- Matching Pattern in String:
string match: Checks if a string matches a pattern.
set string1 "2023@chipworld.com"
set string2 "@.com"
if {[string match "*@*.com" $string1]} {
puts "match found"
} else {
puts "match not found"
}
;# Output: match found
if {[string match "*.com" $string1]} {
puts "match found"
} else {
puts "match not found"
}
;# Output: match found
if {[string match $string2 $string1]} {
puts "match found"
} else {
puts "match not found"
}
;# Output: match not found
if {[string match "chip" $string1]} {
puts "match found"
} else {
puts "match not found"
}
;# Output: match not found
.png)
0 Comments