Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

주어진 문자열에 하위 문자열이 있는지 확인하는 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