string 检查字符串是否包含 Tcl 中的片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41066753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check whether string contains fragment in Tcl
提问by user2300369
I have a set of words, e.g. {6-31G*, 6-311G*, 6-31++G*, 6-311++G**}. As you may see, the common fragment is "6-31". What I need to do in Tcl now is to check whether string under $variable
contains this fragment. I know I could do it with regular expression like this:
我有一组词,例如 {6-31G*, 6-311G*, 6-31++G*, 6-311++G**}。如您所见,常见的片段是“6-31”。我现在需要在 Tcl 中做的是检查下的字符串是否$variable
包含此片段。我知道我可以用这样的正则表达式来做到这一点:
if {[regexp {^6-31} $variable]} {
puts "You provided Pople-style basis set"
}
but what other solution could I use (just out of curiosity)?
但是我可以使用其他什么解决方案(只是出于好奇)?
回答by glenn Hymanman
Just to check if a string contains a particular substring, I'd use string first
只是为了检查字符串是否包含特定的子字符串,我会使用 string first
set substring "6-31"
if {[string first $substring $variable] != -1} {
puts "\"$substring\" found in \"$variable\""
}
You can also use glob-matching with string match
or switch
您还可以使用全局匹配string match
或switch
switch -glob -- $variable {
*$substring* {puts "found"}
default {puts "not found"}
}