Ticker

6/recent/ticker-posts

Mastering Tcl: A Beginner's Guide to Tool Command Language part 5


String structure

  1. Compare Two Strings:
    • string compare: Compares two strings and returns:
      • 0 if the strings are equal.
      • -1 if the first string is less than the second string.
      • 1 if 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
    
  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

    
  1. String Length:
    • string length: Returns the length of the string.
 puts [string length "Hallo world"]    ;# Output: 11

    
  1. 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

    
  1. String to Lowercase:
    • string tolower: Converts the string to lowercase.
 puts [string tolower "HALLO WORLD"]    ;# Output: hallo world

    
  1. String to Uppercase:
    • string toupper: Converts the string to uppercase.
 puts [string toupper "hallo world"]    ;# Output: HALLO WORLD

    
  1. 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

    
  1. 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

    
  1. 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
    
  1. 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
    

Post a Comment

0 Comments