📜  Tcl-字符串

📅  最后修改于: 2020-10-16 06:27:41             🧑  作者: Mango


Tcl的原始数据类型是字符串,通常我们可以在Tcl上找到仅字符串的引号。这些字符串可以包含字母数字字符,仅数字,布尔值甚至二进制数据。 Tcl使用16位unicode字符,字母数字字符可以包含包括非拉丁字符,数字或标点符号的字母。

布尔值可以表示为1,如果是,则为true或true;如果为false,则可以表示为0,no,或者false。

字符串表示

与其他语言不同,在Tcl中,当它只是一个单词时,不需要双引号。一个例子可以是-

现场演示

#!/usr/bin/tclsh

set myVariable hello
puts $myVariable

执行以上代码后,将产生以下结果-

hello

当我们要表示多个字符串,可以使用双引号或花括号。它显示如下-

现场演示

#!/usr/bin/tclsh

set myVariable "hello world"
puts $myVariable
set myVariable {hello world}
puts $myVariable

执行以上代码后,将产生以下结果-

hello world
hello world

字符串转义序列

字符字面量可以是普通字符(例如’x’),转义序列(例如’\ t’)或通用字符(例如’\ u02C0’)。

在Tcl中,当某些字符前面带有反斜杠时,它们将具有特殊含义,并且用于表示换行符(\ n)或制表符(\ t)。在这里,您有一些此类转义序列代码的列表-

Escape sequence Meaning
\\ \ character
\’ ‘ character
\” ” character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab

以下是显示一些转义序列字符的示例-

现场演示

#!/usr/bin/tclsh

puts "Hello\tWorld\n\nTutorialspoint";

编译并执行上述代码后,将产生以下结果-

Hello   World

Tutorialspoint

字符串命令

下表列出了字符串命令的子命令列表-

Sr.No. Methods & Description
1

compare string1 string2

Compares string1 and string2 lexographically. Returns 0 if equal, -1 if string1 comes before string2, else 1.

2

first string1 string2

Returns the index first occurrence of string1 in string2. If not found, returns -1.

3

index string index

Returns the character at index.

4

last string1 string2

Returns the index last occurrence of string1 in string2. If not found, returns -1.

5

length string

Returns the length of string.

6

match pattern string

Returns 1 if the string matches the pattern.

7

range string index1 index2

Return the range of characters in string from index1 to index2.

8

tolower string

Returns the lowercase string.

9

toupper string

Returns the uppercase string.

10

trim string ?trimcharacters?

Removes trimcharacters in both ends of string. The default trimcharacters is whitespace.

11

trimleft string ?trimcharacters?

Removes trimcharacters in left beginning of string. The default trimcharacters is whitespace.

12

trimright string ?trimcharacters?

Removes trimcharacters in left end of string. The default trimcharacters is whitespace.

13

wordend findstring index

Return the index in findstring of the character after the word containing the character at index.

14

wordstart findstring index

Return the index in findstring of the first character in the word containing the character at index.

下面给出了一些常用的Tcl字符串子命令的示例。

字符串比较

#!/usr/bin/tclsh

set s1 "Hello"
set s2 "World"
set s3 "World"
puts [string compare $s1 $s2]
if {[string compare $s2 $s3] == 0} {
   puts "String \'s1\' and \'s2\' are same.";
}

if {[string compare $s1 $s2] == -1} {
   puts "String \'s1\' comes before \'s2\'.";
}

if {[string compare $s2 $s1] == 1} {
   puts "String \'s2\' comes after \'s1\'.";
}

编译并执行上述代码后,将产生以下结果-

-1
String 's1' and 's2' are same.
String 's1' comes before 's2'.
String 's2' comes after 's1'.

字符串索引

现场演示

#!/usr/bin/tclsh

set s1 "Hello World"
set s2 "o"
puts "First occurrence of $s2 in s1"
puts [string first $s2 $s1]
puts "Character at index 0 in s1"
puts [string index $s1 0]
puts "Last occurrence of $s2 in s1"
puts [string last $s2 $s1]
puts "Word end index in s1"
puts [string wordend $s1 20]
puts "Word start index in s1"
puts [string wordstart $s1 20]

编译并执行上述代码后,将产生以下结果-

First occurrence of o in s1
4
Character at index 0 in s1
H
Last occurrence of o in s1
7
Word end index in s1
11
Word start index in s1
6

线长

现场演示

#!/usr/bin/tclsh

set s1 "Hello World"
puts "Length of string s1"
puts [string length $s1]

编译并执行上述代码后,将产生以下结果-

Length of string s1
11

处理案件

现场演示

#!/usr/bin/tclsh

set s1 "Hello World"
puts "Uppercase string of s1"
puts [string toupper $s1]
puts "Lowercase string of s1"
puts [string tolower $s1]

编译并执行上述代码后,将产生以下结果-

Uppercase string of s1
HELLO WORLD
Lowercase string of s1
hello world

修剪字符

现场演示

#!/usr/bin/tclsh

set s1 "Hello World"
set s2 "World"
puts "Trim right $s2 in $s1"
puts [string trimright $s1 $s2]

set s2 "Hello"
puts "Trim left $s2 in $s1"
puts [string trimleft $s1 $s2]

set s1 " Hello World "
set s2 " "
puts "Trim characters s1 on both sides of s2"
puts [string trim $s1 $s2]

编译并执行上述代码后,将产生以下结果-

Trim right World in Hello World
Hello 
Trim left Hello in Hello World
 World
Trim characters s1 on both sides of s2
Hello World

匹配字符串

现场演示

#!/usr/bin/tclsh

set s1 "test@test.com" 
set s2 "*@*.com"
puts "Matching pattern s2 in s1"
puts [string match "*@*.com" $s1 ]
puts "Matching pattern tcl in s1"
puts [string match {tcl} $s1]

编译并执行上述代码后,将产生以下结果-

Matching pattern s2 in s1
1
Matching pattern tcl in s1
0

追加命令

现场演示

#!/usr/bin/tclsh

set s1 "Hello" 
append s1 " World"
puts $s1

编译并执行上述代码后,将产生以下结果-

Hello World

格式化命令

下表显示了Tcl中可用的格式说明符列表-

Specifier Use
%s String representation
%d Integer representation
%f Floating point representation
%e Floating point representation with mantissa-exponent form
%x Hexa decimal representation

一些简单的例子如下-

现场演示

#!/usr/bin/tclsh

puts [format "%f" 43.5]
puts [format "%e" 43.5]
puts [format "%d %s" 4 tuts]
puts [format "%s" "Tcl Language"]
puts [format "%x" 40]

编译并执行上述代码后,将产生以下结果-

43.500000
4.350000e+01
4 tuts
Tcl Language
28

扫描指令

扫描命令用于根据格式说明符解析字符串。一些示例如下所示。

现场演示

#!/usr/bin/tclsh

puts [scan "90" {%[0-9]} m]
puts [scan "abc" {%[a-z]} m]
puts [scan "abc" {%[A-Z]} m]
puts [scan "ABC" {%[A-Z]} m]

编译并执行上述代码后,将产生以下结果-

1
1
0
1