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

更新于:19-Jun-2020

497 次浏览

开启你的 职业生涯

完成课程,获取认证

开始
Advertisement
© . All rights reserved.