TCL 仅检查空格
TCL check for only whitespace
我正在构建代码以将用户输入添加到文件中,但我想捕获用户仅输入空格而不输入其他内容的事件。我怎么做?目前我正在对“”和“”进行硬编码,如果用户输入一个或两个空格,它们将被捕获,但我相信有比我更好的解决方案。
将用户输入插入文本文件的过程
proc inputWords {entryWidget} {
set inputs [$entryWidget get]
$entryWidget delete 0 end
if {$inputs == ""} {
.messageText configure -text "No empty strings"
} elseif {$inputs == " " || $inputs == " "} {
.messageText configure -text "No whitespace strings"
} else {
set sp [open textfile.txt a]
puts $sp $inputs
close $sp
.messageText configure -text "Added $inputs into text file."
}
}
GUI代码
button .messageText -text "Add words" -command "inputWords .ent"
entry .ent
pack .messageText .ent
您可以使用像 {^\s+$} 这样的正则表达式,它匹配字符串的开头,后跟一个或多个白色 spaces(space 或制表符)直到结尾字符串。所以在你的例子中:
elseif {[regexp {^\s+$} $inputs]} {
.messageText configure -text "No whitespace strings"
...
如果要检查同一表达式中的所有白色space 和空字符串,请使用{^\s*$}。
有关 TCL 中正则表达式的详细信息,请参阅 http://wiki.tcl.tk/396。如果这是您第一次接触正则表达式,我建议您在线查找正则表达式教程。
假设您想要 trim 关闭用户输入的前导和尾随空格,您可以 trim 字符串并检查零长度。性能方面,这个更好:
% set inputs " "
% string length $inputs
4
% string length [string trim $inputs]
0
%
% time {string length [string trim $inputs]} 1000
2.315 microseconds per iteration
% time {regexp {^\s+$} $inputs} 1000
3.173 microseconds per iteration
% time {string length [string trim $inputs]} 10000
1.8305 microseconds per iteration
% time {regexp {^\s+$} $inputs} 10000
3.1686 microseconds per iteration
%
% # Trim it once and use it for calculating length
% set foo [string trim $inputs]
% time {string length $foo} 1000
1.596 microseconds per iteration
% time {string length $foo} 10000
1.4619 microseconds per iteration
%
接受任意长度的空白字符串,包括0:
string is space $inputs
接受非空的空白字符串:
string is space -strict $inputs
结果为真 (=1) 或假 (=0)。
文档:string
我正在构建代码以将用户输入添加到文件中,但我想捕获用户仅输入空格而不输入其他内容的事件。我怎么做?目前我正在对“”和“”进行硬编码,如果用户输入一个或两个空格,它们将被捕获,但我相信有比我更好的解决方案。
将用户输入插入文本文件的过程
proc inputWords {entryWidget} {
set inputs [$entryWidget get]
$entryWidget delete 0 end
if {$inputs == ""} {
.messageText configure -text "No empty strings"
} elseif {$inputs == " " || $inputs == " "} {
.messageText configure -text "No whitespace strings"
} else {
set sp [open textfile.txt a]
puts $sp $inputs
close $sp
.messageText configure -text "Added $inputs into text file."
}
}
GUI代码
button .messageText -text "Add words" -command "inputWords .ent"
entry .ent
pack .messageText .ent
您可以使用像 {^\s+$} 这样的正则表达式,它匹配字符串的开头,后跟一个或多个白色 spaces(space 或制表符)直到结尾字符串。所以在你的例子中:
elseif {[regexp {^\s+$} $inputs]} {
.messageText configure -text "No whitespace strings"
...
如果要检查同一表达式中的所有白色space 和空字符串,请使用{^\s*$}。
有关 TCL 中正则表达式的详细信息,请参阅 http://wiki.tcl.tk/396。如果这是您第一次接触正则表达式,我建议您在线查找正则表达式教程。
假设您想要 trim 关闭用户输入的前导和尾随空格,您可以 trim 字符串并检查零长度。性能方面,这个更好:
% set inputs " "
% string length $inputs
4
% string length [string trim $inputs]
0
%
% time {string length [string trim $inputs]} 1000
2.315 microseconds per iteration
% time {regexp {^\s+$} $inputs} 1000
3.173 microseconds per iteration
% time {string length [string trim $inputs]} 10000
1.8305 microseconds per iteration
% time {regexp {^\s+$} $inputs} 10000
3.1686 microseconds per iteration
%
% # Trim it once and use it for calculating length
% set foo [string trim $inputs]
% time {string length $foo} 1000
1.596 microseconds per iteration
% time {string length $foo} 10000
1.4619 microseconds per iteration
%
接受任意长度的空白字符串,包括0:
string is space $inputs
接受非空的空白字符串:
string is space -strict $inputs
结果为真 (=1) 或假 (=0)。
文档:string