String.Contains 메서드

정의

오버로드

Contains(Char)

이 문자열 내에서 지정한 문자가 표시되는지를 나타내는 값을 반환합니다.

Contains(String)

이 문자열 내에서 지정한 하위 문자열이 발생하는지를 나타내는 값을 반환합니다.

Contains(Char, StringComparison)

지정된 비교 규칙을 사용하여 이 문자열 내에서 지정한 문자가 표시되는지를 나타내는 값을 반환합니다.

Contains(String, StringComparison)

지정된 비교 규칙을 사용하여 이 문자열 내에서 지정한 문자열이 표시되는지를 나타내는 값을 반환합니다.

Contains(Char)

Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

이 문자열 내에서 지정한 문자가 표시되는지를 나타내는 값을 반환합니다.

public:
 bool Contains(char value);
public bool Contains (char value);
member this.Contains : char -> bool
Public Function Contains (value As Char) As Boolean

매개 변수

value
Char

찾을 문자입니다.

반환

이 문자열에 value 매개 변수가 표시되면 true이고, 그렇지 않으면 false입니다.

설명

이 메서드는 서수(대/소문자를 구분하고 문화권을 구분하지 않음) 비교를 수행합니다.

적용 대상

Contains(String)

Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

이 문자열 내에서 지정한 하위 문자열이 발생하는지를 나타내는 값을 반환합니다.

public:
 bool Contains(System::String ^ value);
public bool Contains (string value);
member this.Contains : string -> bool
Public Function Contains (value As String) As Boolean

매개 변수

value
String

검색할 문자열입니다.

반환

이 문자열 내에서 true 매개 변수가 발생하거나 value가 빈 문자열("")이면 value이고, 그러지 않으면 false입니다.

예외

value이(가) null인 경우

예제

다음 예제에서는 문자열 "fox"가 친숙한 따옴표의 부분 문자열인지 여부를 확인합니다. 문자열에 "fox"가 있으면 시작 위치도 표시됩니다.

using namespace System;

int main()
{
   String^ s1 = "The quick brown fox jumps over the lazy dog";
   String^ s2 = "fox";
   bool b = s1->Contains( s2 );
   Console::WriteLine( "Is the string, s2, in the string, s1?: {0}", b );
   if (b) {
      int index = s1->IndexOf(s2);
      if (index >= 0)
         Console::WriteLine("'{0} begins at character position {1}",
                            s2, index + 1);
   }
}
// This example displays the following output:
//    'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
//    'fox begins at character position 17
string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox";
bool b = s1.Contains(s2);
Console.WriteLine("'{0}' is in the string '{1}': {2}",
                s2, s1, b);
if (b) {
    int index = s1.IndexOf(s2);
    if (index >= 0)
        Console.WriteLine("'{0} begins at character position {1}",
                      s2, index + 1);
}
// This example displays the following output:
//    'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
//    'fox begins at character position 17
let s1 = "The quick brown fox jumps over the lazy dog"
let s2 = "fox"
let b = s1.Contains s2
printfn $"'{s2}' is in the string '{s1}': {b}"
if b then
    let index = s1.IndexOf s2
    if index >= 0 then
        printfn $"'{s2} begins at character position {index + 1}"
// This example displays the following output:
//    'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
//    'fox begins at character position 17
Class Example
   Public Shared Sub Main()
      Dim s1 As String = "The quick brown fox jumps over the lazy dog"
      Dim s2 As String = "fox"
      Dim b As Boolean = s1.Contains(s2)
      Console.WriteLine("'{0}' is in the string '{1}': {2}",
                        s2, s1, b)
      If b Then
          Dim index As Integer = s1.IndexOf(s2)
          If index >= 0 Then
             Console.WriteLine("'{0} begins at character position {1}",
                               s2, index + 1)
          End If
       End If
   End Sub
End Class
'
' This example displays the following output:
'    'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
'    'fox begins at character position 17

설명

이 메서드는 서수(대/소문자를 구분하고 문화권을 구분하지 않음) 비교를 수행합니다. 검색은 이 문자열의 첫 번째 문자 위치에서 시작하여 마지막 문자 위치를 계속 진행합니다.

문화권 구분 또는 서수 대/소문자를 구분하지 않는 비교를 수행하려면 다음을 수행합니다.

  • .NET Core 2.1 이상 버전에서: 대신 오버로드를 Contains(String, StringComparison) 호출합니다.

  • .NET Framework: 사용자 지정 메서드를 만듭니다. 다음 예제에서는 이러한 방법 중 하나를 보여 줍니다. 매개 변수를 String 포함하는 확장 메서드를 StringComparison 정의하고 지정된 형식의 문자열 비교를 사용할 때 문자열에 부분 문자열이 포함되어 있는지 여부를 나타냅니다.

using System;

public static class StringExtensions
{
   public static bool Contains(this String str, String substring, 
                               StringComparison comp)
   {                            
        if (substring == null)
            throw new ArgumentNullException("substring", 
                                         "substring cannot be null.");
        else if (! Enum.IsDefined(typeof(StringComparison), comp))
            throw new ArgumentException("comp is not a member of StringComparison",
                                     "comp");

        return str.IndexOf(substring, comp) >= 0;                      
   }
}
open System
open System.Runtime.CompilerServices

[<Extension>]
type StringExtensions =
    [<Extension>]
    static member Contains(str: string, substring, comp: StringComparison) =
        if substring = null then
            invalidArg "substring" "substring cannot be null"
        if Enum.IsDefined(typeof<StringComparison>, comp) |> not then
            invalidArg "comp" "comp is not a member of StringComparison"
        str.IndexOf(substring, comp) >= 0
String s = "This is a string.";
String sub1 = "this";
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
StringComparison comp = StringComparison.Ordinal;
Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));

comp = StringComparison.OrdinalIgnoreCase;
Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));

// The example displays the following output:
//       Does 'This is a string.' contain 'this'?
//          Ordinal: False
//          OrdinalIgnoreCase: True
let s = "This is a string."
let sub1 = "this"
printfn $"Does '{s}' contain '{sub1}'?"
let comp = StringComparison.Ordinal
printfn $"   {comp:G}: {s.Contains(sub1, comp)}"

let comp2 = StringComparison.OrdinalIgnoreCase
printfn $"   {comp2:G}: {s.Contains(sub1, comp2)}"

// The example displays the following output:
//       Does 'This is a string.' contain 'this'?
//          Ordinal: False
//          OrdinalIgnoreCase: True
Imports System.Runtime.CompilerServices

Module StringExtensions
   <Extension()>
   Public Function Contains(str As String, substring As String, 
                            comp As StringComparison) As Boolean
      If substring Is Nothing Then
         Throw New ArgumentNullException("substring", 
                                         "substring cannot be null.")
      Else If Not [Enum].IsDefined(GetType(StringComparison), comp)
         Throw New ArgumentException("comp is not a member of StringComparison",
                                     "comp")
      End If                               
      Return str.IndexOf(substring, comp) >= 0                      
   End Function
End Module
Public Module Example
   Public Sub Main
      Dim s As String = "This is a string."
      Dim sub1 As String = "this"
      Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1)
      Dim comp As StringComparison = StringComparison.Ordinal
      Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp))
      
      comp = StringComparison.OrdinalIgnoreCase
      Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp))
   End Sub
End Module
' The example displays the following output:
'       Does 'This is a string.' contain 'this'?
'          Ordinal: False
'          OrdinalIgnoreCase: True

현재 instance 부분 문자열 value 의 위치에 관심이 있는 경우 메서드를 호출 IndexOf 하여 첫 번째 발생의 시작 위치를 얻거나 메서드를 호출 LastIndexOf 하여 마지막 발생의 시작 위치를 가져올 수 있습니다. 이 예제에서는 문자열 instance 부분 문자열이 있는 경우 메서드에 대한 호출 IndexOf(String) 을 포함합니다.

추가 정보

적용 대상

Contains(Char, StringComparison)

Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

지정된 비교 규칙을 사용하여 이 문자열 내에서 지정한 문자가 표시되는지를 나타내는 값을 반환합니다.

public:
 bool Contains(char value, StringComparison comparisonType);
public bool Contains (char value, StringComparison comparisonType);
member this.Contains : char * StringComparison -> bool
Public Function Contains (value As Char, comparisonType As StringComparison) As Boolean

매개 변수

value
Char

찾을 문자입니다.

comparisonType
StringComparison

비교에 사용할 규칙을 지정하는 열거형 값 중 하나입니다.

반환

이 문자열에 value 매개 변수가 표시되면 true이고, 그렇지 않으면 false입니다.

적용 대상

Contains(String, StringComparison)

Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

지정된 비교 규칙을 사용하여 이 문자열 내에서 지정한 문자열이 표시되는지를 나타내는 값을 반환합니다.

public:
 bool Contains(System::String ^ value, StringComparison comparisonType);
public bool Contains (string value, StringComparison comparisonType);
member this.Contains : string * StringComparison -> bool
Public Function Contains (value As String, comparisonType As StringComparison) As Boolean

매개 변수

value
String

검색할 문자열입니다.

comparisonType
StringComparison

비교에 사용할 규칙을 지정하는 열거형 값 중 하나입니다.

반환

이 문자열 내에서 true 매개 변수가 발생하거나 value가 빈 문자열("")이면 value이고, 그러지 않으면 false입니다.

적용 대상