必须搜索子字符串是否存在于字符串中

发布于 2025-01-13 19:39:17 字数 524 浏览 3 评论 0原文

我有几个子字符串要在下面的字符串中搜索

These are the substrings "School, Students, Teachers, Classrooms"

下面是这里的字符串我搜索上面的子字符串是否出现在下面的字符串中

School_Name
Number_Of_Students
Number_Of_Teachers
Number_Of_Classrooms

-我已经尝试过这种方式,在字符串中我有学生、教师、教室存在子字符串,但我无法正确搜索它

#!/usr/bin/tclsh
set list {School Students Teachers Classrooms}
set string "Number_Of_Students Number_Of_Teachers Number_Of_Classrooms"
if {"$string" in $list} {
puts "$string"
}

任何人都可以帮我吗

I have a few substrings to search in the below following strings

These are the substrings "School, Students, Teachers, Classrooms"

Below are the strings here I have the search whether the above substrings are present in the below strings-

School_Name
Number_Of_Students
Number_Of_Teachers
Number_Of_Classrooms

I have tried in this way, here in the string I have Students, Teachers, Classrooms substrings present, but I'm not able to search it properly

#!/usr/bin/tclsh
set list {School Students Teachers Classrooms}
set string "Number_Of_Students Number_Of_Teachers Number_Of_Classrooms"
if {"$string" in $list} {
puts "$string"
}

Can anyone help me out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-01-20 19:39:17

in 运算符only 执行字符串与列表元素的精确匹配。您可能希望将 lsearchstring match 样式的 glob 模式一起使用;他们很容易合作。或者也许只是字符串匹配和一些foreach循环;我不确定模式列表是什么以及搜索空间列表是什么。

# Added extra item to show what happens with multiple matches
set inputs {
    School_Name
    Number_Of_Students
    Number_Of_Teachers
    Number_Of_Classrooms
    Students_Per_School
}
set items {School Students Teachers Classrooms}
foreach input $inputs {
    foreach item $items {
        if {[string match *$item* $input]} {
            puts "found $item in $input"
        }
    }
}

The in operator only does exact matching of a string against elements of a list. You probably want to use lsearch with a string match-style glob pattern; they're fairly easy to work with. Or maybe just string match and some foreach loops; I'm not sure what are the list of patterns and what are the list that is the search space.

# Added extra item to show what happens with multiple matches
set inputs {
    School_Name
    Number_Of_Students
    Number_Of_Teachers
    Number_Of_Classrooms
    Students_Per_School
}
set items {School Students Teachers Classrooms}
foreach input $inputs {
    foreach item $items {
        if {[string match *$item* $input]} {
            puts "found $item in $input"
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文