2014-04-15

TCL, string, compare & equal 字串處理/比對

比對字串是以 Unicode 逐一比對 string1 & string2.

compare: string1 內含字元大於 string2, 則回傳 1
compare: string1 內含字元相同 string2, 則回傳 0
compare: string1 內含字元小於 string2, 則回傳 -1
equal: string1 完全等於 string2, 則回傳 1, 反之回傳 0

syntax]:
  • string  compare  string1  string2
  • string  equal  string1  string2
parameter]:
     - nocase | 比對時不分大小寫
     - length | 決定比對的長度 "length  len"

example]:
     set s2 "Hello Tcl !!"
     set s3 "Hello tcl !!"

     puts [string compare $s2 $s3]
     puts [string compare -nocase $s2 $s3]
     puts [string equal  $s2 $s3]
     puts [string equal -nocase $s2 $s3]

output]:
     -1       #<S2 Tcl 與 S3 tcl 的 T/t 不同, T 在 unicode 的十進制為 84, t 為 116, 所以 s2 小於 s3.>
     0        #<如不分大小寫, 則 S2 & S3 字串大小相同.>
     0        #<S2 & S3 字串不相同, 參數 equal 回傳 0.>
     1        #<不分大小寫後, S2 & S3 字串就相同, 則回傳 1.>



沒有留言:

張貼留言