C# 程序检查子字符串是否在给定字符串中
使用 C# 中的 contains() 方法检查子字符串是否在给定字符串中。
假设字符串是 −
United
在字符串内,您需要找到子字符串“Uni”。为此,请使用 contains 方法并像以下代码片段一样使用 −
res = str1.Contains(str2);
示例
您可以尝试运行以下代码在字符串中查找子字符串。
using System;
public class Demo {
public static void Main() {
string str1 = "United", str2 = "Uni";
bool res;
res = str1.Contains(str2);
if (res)
Console.Write("The substring " + str2 + " is in the string " + str1);
else
Console.Write("The substring " + str2 + " is not in the string " + str1);
}
}输出
The substring Uni is in the string United
Advertisement
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP