gpt4 book ai didi

tcl - Tcl 中不区分大小写的比较

转载 作者:行者123 更新时间:2023-12-02 08:34:49 30 4
gpt4 key购买 nike

我在 TCL 中有代码:

set a 1
set b 0
set c "Start"
if { $a == 1 && ($b == 1 || $c == "Start") } {
puts Works
}

我想让 $c == "Start" 来检查不区分大小写的 Start。我该怎么做?

最佳答案

您可以使用 string compare :

string compare ?-nocase? ?-length int? string1 string2

Perform a character-by-character comparison of strings string1 and string2. Returns -1, 0, or 1, depending on whether string1 is lexicographically less than, equal to, or greater than string2. If -length is specified, then only the first length characters are used in the comparison. If -length is negative, it is ignored. If -nocase is specified, then the strings are compared in a case-insensitive manner.

因此,如果它返回 0,则 $c 完全等于 String

set a 1
set b 0
set c "Start"
if { $a == 1 && ($b == 1 || [string compare -nocase $c "Start"] == 0) } {
puts Works
}

-nocase 开关确保它使用文档中提到的不区分大小写的比较。


另一种方法是将 $c 设为统一大小写,并使用 Start 也采用统一大小写。例如,您可以将所有内容都转换为小写:

set a 1
set b 0
set c "Start"
if { $a == 1 && ($b == 1 || [string tolower $c] == "start") } {
puts Works
}

或大写...

set a 1
set b 0
set c "Start"
if { $a == 1 && ($b == 1 || [string toupper $c] == "START") } {
puts Works
}

如果您不介意“探索”那个领域,另一种选择是使用 regexp

set a 1
set b 0
set c "Start"
if { $a == 1 && ($b == 1 || [regexp -nocase -- {^Start$} $c]) } {
puts Works
}

regexp 为匹配返回 1,为不匹配返回 0^$ 确保整个 Start$c 变量匹配。结论,如果 $cStart 相同,则得到 1

关于tcl - Tcl 中不区分大小写的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868768/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com