Python是否具有字符串“包含”子字符串方法?

发布于 2021-02-02 23:23:45

我在寻找Python中的string.containsor string.indexof方法。

我想要做:

if not somestring.contains("blah"):
   continue
关注者
0
被浏览
140
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    你可以使用in运算符

    if "blah" not in somestring: 
        continue
    


  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    如果只是子字符串搜索,则可以使用string.find("substring")

    你必须与小心一点find,index和in虽然,因为它们是字符串搜索。换句话说,这是:

    s = "This be a string"
    if s.find("is") == -1:
        print "No 'is' here!"
    else:
        print "Found 'is' in the string."
    

    它将打印Found 'is' in the string.类似,if "is" in s:结果为True。这可能是你想要的,也可能不是。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看