首页
学习
活动
专区
工具
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 等)来检查一个字符串是否包含另一个字符串。这些函数通常返回一个布尔值,表示是否找到了子字符串。

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

相关·内容

没有搜到相关的合辑

领券