package require Tcl
package require Tk
set str "This is a string"
puts "The string is :$str"
puts "The length of the string is [string length $str]"
puts "The character at index 3 is:[string index $str 3]"
puts "The character from index 4 through 8 are:[string range $str 4 8]"
puts "The index of the first occurrence of letter \"i\" is :[string first i $str]"
输出
The string is :This is a string
The length of the string is 16
The character at index 3 is:s
The character from index 4 through 8 are: is a
The index of the first occurrence of letter "i" is :2
结果分析[string index $str3]可以用来显示字符串中第四个字符为何(第一个字母的index为0),[string range $str 4 8]用以显示字符串的第五个到第九个字符,[string first i $str]用来显示字母i在字符串中第一次出现时的index值。
阅读(1696) | 评论(0) | 转发(0) |