首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

检查string是否包含另一个字符串

在编程中,我们经常需要检查一个字符串(string)是否包含另一个字符串。这可以通过使用编程语言中的字符串查找函数来实现。以下是一些常见编程语言中如何检查字符串是否包含另一个字符串的示例:

  1. Python:string = "Hello, world!" substring = "world" if substring in string: print("Substring is found!") else: print("Substring is not found!")
  2. Java:String string = "Hello, world!"; String substring = "world"; if (string.contains(substring)) { System.out.println("Substring is found!"); } else { System.out.println("Substring is not found!"); }
  3. JavaScript:let string = "Hello, world!"; let substring = "world"; if (string.includes(substring)) { console.log("Substring is found!"); } else { console.log("Substring is not found!"); }
  4. C#:string str = "Hello, world!"; string subStr = "world"; if (str.Contains(subStr)) { Console.WriteLine("Substring is found!"); } else { Console.WriteLine("Substring is not found!"); }
  5. PHP:$string = "Hello, world!"; $substring = "world"; if (strpos($string, $substring) !== false) { echo "Substring is found!"; } else { echo "Substring is not found!"; }

在这些示例中,我们使用了不同编程语言中的字符串查找函数(如 in, contains, includes, strpos 等)来检查一个字符串是否包含另一个字符串。这些函数通常返回一个布尔值,表示是否找到了子字符串。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • LeetCode笔记:242. Valid Anagram

    一开始,想了一个现在看来很笨的办法,这道题无非就是要检查两个字符串中的字母是否全部一致,我就遍历其中一个字符串,在每一个字符中,从另一个字符串找到第一个相同的字符,然后删掉字符串中的这个字符,继续遍历,直到有一个字符在另一个字符串中找不到了,说明没有这个字符或者数量少一些,就返回false,如果全部遍历完了都找得到,且另一个字符串也被删完了,那就返回true。这个办法我提交之后,很悲剧的超时了。。。想想也是,时间复杂度是n的平方了,还是很大的。 后来想到了另一个方法,我弄两个int数组,初始各自包含26个"0",用来记录两个字符串中各个字母出现的次数,然后分别遍历两个数组,记录其各个字母出现的次数,最后比较两个int数组是否完全一致就可以了,一遍ac,耗时5ms,打败了85%的提交者,哈哈哈。

    01
    领券