如何在 C# 中从字符串中查找子字符串?
设置字符串
string s = "Tom Cruise";
现在,假设你需要查找子字符串“Tom”,那么请使用 Contains() 方法。
if (s.Contains("Tom") == true) {
Console.WriteLine("Substring found!");
}以下是如何查找字符串中子字符串的代码 -
示例
using System;
public class Demo {
public static void Main() {
string s = "Tom Cruise";
if (s.Contains("Tom") == true) {
Console.WriteLine("Substring found!");
} else {
Console.WriteLine("Substring not found!");
}
}
}输出
Substring found!
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP