如何在 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!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP