String.StartsWith 메서드

정의

오버로드

StartsWith(String, Boolean, CultureInfo)

지정한 문화권을 사용하여 비교할 때 이 문자열 인스턴스의 시작 부분과 지정한 문자열이 일치하는지를 확인합니다.

StartsWith(String, StringComparison)

지정한 비교 옵션을 사용하여 비교할 때 지정한 문자열과 이 문자열 인스턴스의 시작 부분이 일치하는지를 확인합니다.

StartsWith(Char)

이 문자열 인스턴스가 지정한 문자로 시작하는지를 확인합니다.

StartsWith(String)

이 문자열 인스턴스의 시작 부분과 지정한 문자열이 일치하는지를 확인합니다.

StartsWith(String, Boolean, CultureInfo)

지정한 문화권을 사용하여 비교할 때 이 문자열 인스턴스의 시작 부분과 지정한 문자열이 일치하는지를 확인합니다.

public:
 bool StartsWith(System::String ^ value, bool ignoreCase, System::Globalization::CultureInfo ^ culture);
public bool StartsWith (string value, bool ignoreCase, System.Globalization.CultureInfo? culture);
public bool StartsWith (string value, bool ignoreCase, System.Globalization.CultureInfo culture);
member this.StartsWith : string * bool * System.Globalization.CultureInfo -> bool
Public Function StartsWith (value As String, ignoreCase As Boolean, culture As CultureInfo) As Boolean

매개 변수

value
String

비교할 문자열입니다.

ignoreCase
Boolean

비교 시 대/소문자를 무시하려면 true이고, 그러지 않으면 false입니다.

culture
CultureInfo

이 문자열과 value의 비교 방법을 결정하는 문화권 정보입니다. culturenull이면 현재 문화권이 사용됩니다.

반환

이 문자열의 시작 부분이 true 매개 변수와 일치하면 value이고, 그러지 않으면 false입니다.

예외

valuenull입니다.

예제

다음 예제에서는 문자열이 다른 문자열의 시작 부분에서 발생하는지 여부를 결정합니다. 메서드는 StartsWith 대/소문자 구분, 대/소문자 구분 및 검색 결과에 영향을 주는 다양한 문화권을 사용하여 여러 번 호출됩니다.

// This code example demonstrates the 
// System.String.StartsWith(String, ..., CultureInfo) method.

using System;
using System.Threading;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
    string msg1 = "Search for the target string \"{0}\" in the string \"{1}\".\n";
    string msg2 = "Using the {0} - \"{1}\" culture:";
    string msg3 = "  The string to search ends with the target string: {0}";
    bool result = false;
    CultureInfo ci;

// Define a target string to search for.
// U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
    string capitalARing = "\u00c5";

// Define a string to search. 
// The result of combining the characters LATIN SMALL LETTER A and COMBINING 
// RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character 
// LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
    string aRingXYZ = "\u0061\u030a" + "xyz";

// Clear the screen and display an introduction.
    Console.Clear();

// Display the string to search for and the string to search.
    Console.WriteLine(msg1, capitalARing, aRingXYZ);

// Search using English-United States culture.
    ci = new CultureInfo("en-US");
    Console.WriteLine(msg2, ci.DisplayName, ci.Name);

    Console.WriteLine("Case sensitive:");
    result = aRingXYZ.StartsWith(capitalARing, false, ci);
    Console.WriteLine(msg3, result);

    Console.WriteLine("Case insensitive:");
    result = aRingXYZ.StartsWith(capitalARing, true, ci);
    Console.WriteLine(msg3, result);
    Console.WriteLine();

// Search using Swedish-Sweden culture.
    ci = new CultureInfo("sv-SE");
    Console.WriteLine(msg2, ci.DisplayName, ci.Name);

    Console.WriteLine("Case sensitive:");
    result = aRingXYZ.StartsWith(capitalARing, false, ci);
    Console.WriteLine(msg3, result);

    Console.WriteLine("Case insensitive:");
    result = aRingXYZ.StartsWith(capitalARing, true, ci);
    Console.WriteLine(msg3, result);
    }
}

/*
Note: This code example was executed on a console whose user interface 
culture is "en-US" (English-United States).

Search for the target string "Å" in the string "a°xyz".

Using the English (United States) - "en-US" culture:
Case sensitive:
  The string to search ends with the target string: False
Case insensitive:
  The string to search ends with the target string: True

Using the Swedish (Sweden) - "sv-SE" culture:
Case sensitive:
  The string to search ends with the target string: False
Case insensitive:
  The string to search ends with the target string: False

*/
// This code example demonstrates the
// System.String.StartsWith(String, ..., CultureInfo) method.

open System
open System.Globalization
 
[<EntryPoint>]
let main _ =
    let msg1 = "Search for the target string \"{0}\" in the string \"{1}\".\n"
    let msg2 = "Using the {0} - \"{1}\" culture:"
    let msg3 = "  The string to search ends with the target string: {0}"

    // Define a target string to search for.
    // U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
    let capitalARing = "\u00c5"

    // Define a string to search.
    // The result of combining the characters LATIN SMALL LETTER A and COMBINING
    // RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character
    // LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
    let aRingXYZ = "\u0061\u030a" + "xyz"

    // Clear the screen and display an introduction.
    Console.Clear()

    // Display the string to search for and the string to search.
    Console.WriteLine(msg1, capitalARing, aRingXYZ)

    // Search using English-United States culture.
    let ci = CultureInfo "en-US"
    Console.WriteLine(msg2, ci.DisplayName, ci.Name)

    printfn "Case sensitive:"
    let result = aRingXYZ.StartsWith(capitalARing, false, ci)
    Console.WriteLine(msg3, result)

    printfn "Case insensitive:"
    let result = aRingXYZ.StartsWith(capitalARing, true, ci)
    Console.WriteLine(msg3, result)
    printfn ""

    // Search using Swedish-Sweden culture.
    let ci = CultureInfo "sv-SE"
    Console.WriteLine(msg2, ci.DisplayName, ci.Name)

    printfn "Case sensitive:"
    let result = aRingXYZ.StartsWith(capitalARing, false, ci)
    Console.WriteLine(msg3, result)

    printfn "Case insensitive:"
    let result = aRingXYZ.StartsWith(capitalARing, true, ci)
    Console.WriteLine(msg3, result)
    0
(*
Note: This code example was executed on a console whose user interface
culture is "en-US" (English-United States).

Search for the target string "Å" in the string "a°xyz".

Using the English (United States) - "en-US" culture:
Case sensitive:
  The string to search ends with the target string: False
Case insensitive:
  The string to search ends with the target string: True

Using the Swedish (Sweden) - "sv-SE" culture:
Case sensitive:
  The string to search ends with the target string: False
Case insensitive:
  The string to search ends with the target string: False

*)
' This code example demonstrates the 
' System.String.StartsWith(String, ..., CultureInfo) method.

Imports System.Threading
Imports System.Globalization

Class Sample
    Public Shared Sub Main() 
        Dim msg1 As String = "Search for the target string ""{0}"" in the string ""{1}""." & vbCrLf
        Dim msg2 As String = "Using the {0} - ""{1}"" culture:"
        Dim msg3 As String = "  The string to search ends with the target string: {0}"
        Dim result As Boolean = False
        Dim ci As CultureInfo
        
        ' Define a target string to search for.
        ' U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
        Dim capitalARing As String = "Å"
        
        ' Define a string to search. 
        ' The result of combining the characters LATIN SMALL LETTER A and COMBINING 
        ' RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character 
        ' LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
        Dim aRingXYZ As String = "å" & "xyz"
        
        ' Clear the screen and display an introduction.
        Console.Clear()
        
        ' Display the string to search for and the string to search.
        Console.WriteLine(msg1, capitalARing, aRingXYZ)
        
        ' Search using English-United States culture.
        ci = New CultureInfo("en-US")
        Console.WriteLine(msg2, ci.DisplayName, ci.Name)
        
        Console.WriteLine("Case sensitive:")
        result = aRingXYZ.StartsWith(capitalARing, False, ci)
        Console.WriteLine(msg3, result)
        
        Console.WriteLine("Case insensitive:")
        result = aRingXYZ.StartsWith(capitalARing, True, ci)
        Console.WriteLine(msg3, result)
        Console.WriteLine()
        
        ' Search using Swedish-Sweden culture.
        ci = New CultureInfo("sv-SE")
        Console.WriteLine(msg2, ci.DisplayName, ci.Name)
        
        Console.WriteLine("Case sensitive:")
        result = aRingXYZ.StartsWith(capitalARing, False, ci)
        Console.WriteLine(msg3, result)
        
        Console.WriteLine("Case insensitive:")
        result = aRingXYZ.StartsWith(capitalARing, True, ci)
        Console.WriteLine(msg3, result)
    
    End Sub
End Class

'
'Note: This code example was executed on a console whose user interface 
'culture is "en-US" (English-United States).
'
'Search for the target string "Å" in the string "a°xyz".
'
'Using the English (United States) - "en-US" culture:
'Case sensitive:
'  The string to search ends with the target string: False
'Case insensitive:
'  The string to search ends with the target string: True
'
'Using the Swedish (Sweden) - "sv-SE" culture:
'Case sensitive:
'  The string to search ends with the target string: False
'Case insensitive:
'  The string to search ends with the target string: False
'

설명

이 메서드는 value 매개 변수를 와 길이 value가 같은 이 문자열의 시작 부분에 있는 부분 문자열과 비교하고 같은지 여부를 나타내는 값을 반환합니다. 같 value 게 하려면 빈 문자열()String.Empty이어야 합니다. 이 instance 대한 참조이거나 이 instance 시작 부분과 일치해야 합니다.

이 메서드는 지정된 대/소문자 및 문화권을 사용하여 비교를 수행합니다.

적용 대상

StartsWith(String, StringComparison)

지정한 비교 옵션을 사용하여 비교할 때 지정한 문자열과 이 문자열 인스턴스의 시작 부분이 일치하는지를 확인합니다.

public:
 bool StartsWith(System::String ^ value, StringComparison comparisonType);
public bool StartsWith (string value, StringComparison comparisonType);
[System.Runtime.InteropServices.ComVisible(false)]
public bool StartsWith (string value, StringComparison comparisonType);
member this.StartsWith : string * StringComparison -> bool
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.StartsWith : string * StringComparison -> bool
Public Function StartsWith (value As String, comparisonType As StringComparison) As Boolean

매개 변수

value
String

비교할 문자열입니다.

comparisonType
StringComparison

이 문자열과 value를 비교하는 방법을 결정하는 열거형 값 중 하나입니다.

반환

이 인스턴스가 true로 시작하는 경우 value이고, 그러지 않으면 false입니다.

특성

예외

valuenull입니다.

comparisonTypeStringComparison 값이 아닙니다.

예제

다음 예제에서는 "The"라는 단어로 시작하는 긴 문자열의 시작 부분에 있는 문자열 "the"를 검색합니다. 예제의 출력에서 알 수 있듯이 문화권을 구분하지 않지만 대/소문자를 구분하는 비교를 수행하는 메서드에 대한 호출 StartsWith(String, StringComparison) 은 문자열과 일치하지 않지만 문화권 및 대/소문자를 구분하지 않는 비교를 수행하는 호출은 문자열과 일치합니다.

using namespace System;

void main()
{
   String^ title = "The House of the Seven Gables";
   String^ searchString = "the";
   StringComparison comparison = StringComparison::InvariantCulture;
   Console::WriteLine("'{0}':", title);
   Console::WriteLine("   Starts with '{0}' ({1:G} comparison): {2}",
                      searchString, comparison,
                      title->StartsWith(searchString, comparison));

   comparison = StringComparison::InvariantCultureIgnoreCase;
   Console::WriteLine("   Starts with '{0}' ({1:G} comparison): {2}",
                      searchString, comparison,
                      title->StartsWith(searchString, comparison));
   }
// The example displays the following output:
//       'The House of the Seven Gables':
//          Starts with 'the' (InvariantCulture comparison): False
//          Starts with 'the' (InvariantCultureIgnoreCase comparison): True
using System;

public class Example
{
   public static void Main()
   {
      String title = "The House of the Seven Gables";
      String searchString = "the";
      StringComparison comparison = StringComparison.InvariantCulture;
      Console.WriteLine("'{0}':", title);
      Console.WriteLine("   Starts with '{0}' ({1:G} comparison): {2}",
                        searchString, comparison,
                        title.StartsWith(searchString, comparison));

      comparison = StringComparison.InvariantCultureIgnoreCase;
      Console.WriteLine("   Starts with '{0}' ({1:G} comparison): {2}",
                        searchString, comparison,
                        title.StartsWith(searchString, comparison));
   }
}
// The example displays the following output:
//       'The House of the Seven Gables':
//          Starts with 'the' (InvariantCulture comparison): False
//          Starts with 'the' (InvariantCultureIgnoreCase comparison): True
open System

let title = "The House of the Seven Gables"
let searchString = "the"
let comparison = StringComparison.InvariantCulture
printfn $"'{title}':"
printfn $"   Starts with '{searchString}' ({comparison:G} comparison): {title.StartsWith(searchString, comparison)}"

let comparison2 = StringComparison.InvariantCultureIgnoreCase
printfn $"   Starts with '{searchString}' ({comparison2:G} comparison): {title.StartsWith(searchString, comparison2)}"
// The example displays the following output:
//       'The House of the Seven Gables':
//          Starts with 'the' (InvariantCulture comparison): False
//          Starts with 'the' (InvariantCultureIgnoreCase comparison): True
Module Example
   Public Sub Main()
      Dim title As String = "The House of the Seven Gables"
      Dim searchString As String = "the"
      Dim comparison As StringComparison = StringComparison.InvariantCulture
      Console.WriteLine("'{0}':", title)
      Console.WriteLine("   Starts with '{0}' ({1:G} comparison): {2}",
                        searchString, comparison,
                        title.StartsWith(searchString, comparison))

      comparison = StringComparison.InvariantCultureIgnoreCase
      Console.WriteLine("   Starts with '{0}' ({1:G} comparison): {2}",
                        searchString, comparison,
                        title.StartsWith(searchString, comparison))
   End Sub
End Module
' The example displays the following output:
'    'The House of the Seven Gables':
'       Starts with 'the' (InvariantCulture comparison): False
'       Starts with 'the' (InvariantCultureIgnoreCase comparison): True

다음 예제에서는 문자열이 특정 부분 문자열로 시작하는지 여부를 결정합니다. 2차원 문자열 배열을 초기화합니다. 두 번째 차원의 첫 번째 요소는 문자열을 포함하고 두 번째 요소는 첫 번째 문자열의 시작 부분에 검색할 문자열을 포함합니다. 결과는 문화권 선택, 대/소문자 무시 여부 및 서수 비교 수행 여부의 영향을 받습니다. 문자열 instance 합자를 포함하는 경우 문화권에 민감한 연속 문자와의 비교가 성공적으로 일치합니다.

using namespace System;

void main()
{
   array<String^, 2>^ strings = gcnew array<String^, 2> { {"ABCdef", "abc" },                    
                         {"ABCdef", "abc" }, {"œil","oe" }, 
                         { "læring}", "lae" } };
   for (int ctr1 = strings->GetLowerBound(0); ctr1 <= strings->GetUpperBound(0); ctr1++)
   {
         for each (String^ cmpName in Enum::GetNames(StringComparison::typeid))
         { 
            StringComparison strCmp = (StringComparison) Enum::Parse(StringComparison::typeid, 
                                                         cmpName);
            String^ instance = strings[ctr1, 0];
            String^ value = strings[ctr1, 1];
            Console::WriteLine("{0} starts with {1}: {2} ({3} comparison)", 
                               instance, value, 
                               instance->StartsWith(value, strCmp), 
                               strCmp); 
         }
         Console::WriteLine();   
   }   
}

// The example displays the following output:
//       ABCdef starts with abc: False (CurrentCulture comparison)
//       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (InvariantCulture comparison)
//       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (Ordinal comparison)
//       ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//       
//       ABCdef starts with abc: False (CurrentCulture comparison)
//       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (InvariantCulture comparison)
//       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (Ordinal comparison)
//       ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//       
//       œil starts with oe: True (CurrentCulture comparison)
//       œil starts with oe: True (CurrentCultureIgnoreCase comparison)
//       œil starts with oe: True (InvariantCulture comparison)
//       œil starts with oe: True (InvariantCultureIgnoreCase comparison)
//       œil starts with oe: False (Ordinal comparison)
//       œil starts with oe: False (OrdinalIgnoreCase comparison)
//       
//       læring} starts with lae: True (CurrentCulture comparison)
//       læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
//       læring} starts with lae: True (InvariantCulture comparison)
//       læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
//       læring} starts with lae: False (Ordinal comparison)
//       læring} starts with lae: False (OrdinalIgnoreCase comparison)
using System;

public class Example
{
   public static void Main()
   {
      string[,] strings = { {"ABCdef", "abc" },                    
                            {"ABCdef", "abc" },  
                            {"œil","oe" },
                            { "læring}", "lae" } };
      for (int ctr1 = strings.GetLowerBound(0); ctr1 <= strings.GetUpperBound(0); ctr1++)
      {
            foreach (string cmpName in Enum.GetNames(typeof(StringComparison)))
            { 
               StringComparison strCmp = (StringComparison) Enum.Parse(typeof(StringComparison), 
                                                      cmpName);
               string instance = strings[ctr1, 0];
               string value = strings[ctr1, 1];
               Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)", 
                                 instance, value, 
                                 instance.StartsWith(value, strCmp), 
                                 strCmp); 
            }
            Console.WriteLine();   
      }   
   }
}
// The example displays the following output:
//       ABCdef starts with abc: False (CurrentCulture comparison)
//       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (InvariantCulture comparison)
//       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (Ordinal comparison)
//       ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//       
//       ABCdef starts with abc: False (CurrentCulture comparison)
//       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (InvariantCulture comparison)
//       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (Ordinal comparison)
//       ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//       
//       œil starts with oe: True (CurrentCulture comparison)
//       œil starts with oe: True (CurrentCultureIgnoreCase comparison)
//       œil starts with oe: True (InvariantCulture comparison)
//       œil starts with oe: True (InvariantCultureIgnoreCase comparison)
//       œil starts with oe: False (Ordinal comparison)
//       œil starts with oe: False (OrdinalIgnoreCase comparison)
//       
//       læring} starts with lae: True (CurrentCulture comparison)
//       læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
//       læring} starts with lae: True (InvariantCulture comparison)
//       læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
//       læring} starts with lae: False (Ordinal comparison)
//       læring} starts with lae: False (OrdinalIgnoreCase comparison)
open System

let strings =
    array2D 
      [ [ "ABCdef"; "abc" ]
        [ "ABCdef"; "abc" ]
        [ "œil"; "oe" ]
        [ "læring}"; "lae" ] ]

for ctr1 = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    for cmpName in Enum.GetNames typeof<StringComparison> do
        let strCmp = Enum.Parse(typeof<StringComparison>, cmpName) :?> StringComparison
        let instance = strings[ctr1, 0]
        let value = strings[ctr1, 1]
        printfn $"{instance} starts with {value}: {instance.StartsWith(value, strCmp)} ({strCmp} comparison)"
    printfn ""
// The example displays the following output:
//       ABCdef starts with abc: False (CurrentCulture comparison)
//       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (InvariantCulture comparison)
//       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (Ordinal comparison)
//       ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
//       ABCdef starts with abc: False (CurrentCulture comparison)
//       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (InvariantCulture comparison)
//       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
//       ABCdef starts with abc: False (Ordinal comparison)
//       ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
//       œil starts with oe: True (CurrentCulture comparison)
//       œil starts with oe: True (CurrentCultureIgnoreCase comparison)
//       œil starts with oe: True (InvariantCulture comparison)
//       œil starts with oe: True (InvariantCultureIgnoreCase comparison)
//       œil starts with oe: False (Ordinal comparison)
//       œil starts with oe: False (OrdinalIgnoreCase comparison)
//
//       læring} starts with lae: True (CurrentCulture comparison)
//       læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
//       læring} starts with lae: True (InvariantCulture comparison)
//       læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
//       læring} starts with lae: False (Ordinal comparison)
//       læring} starts with lae: False (OrdinalIgnoreCase comparison)
Module Example
   Public Sub Main()
      Dim strings(,) As String = { {"ABCdef", "abc" },                    
                                   {"ABCdef", "abc" },  
                                   {"œil","oe" },
                                   { "læring}", "lae" } }
      For ctr1 As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
            For Each cmpName As String In [Enum].GetNames(GetType(StringComparison)) 
               Dim strCmp As StringComparison = CType([Enum].Parse(GetType(StringComparison), 
                                                      cmpName), StringComparison)
               Dim instance As String = strings(ctr1, 0)
               Dim value As String = strings(ctr1, 1)
               Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)", 
                                 instance, value, 
                                 instance.StartsWith(value, strCmp), 
                                 strCmp) 
            Next
            Console.WriteLine()   
      Next   
   End Sub
End Module
' The example displays the following output:
'       ABCdef starts with abc: False (CurrentCulture comparison)
'       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
'       ABCdef starts with abc: False (InvariantCulture comparison)
'       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
'       ABCdef starts with abc: False (Ordinal comparison)
'       ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
'       
'       ABCdef starts with abc: False (CurrentCulture comparison)
'       ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
'       ABCdef starts with abc: False (InvariantCulture comparison)
'       ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
'       ABCdef starts with abc: False (Ordinal comparison)
'       ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
'       
'       œil starts with oe: True (CurrentCulture comparison)
'       œil starts with oe: True (CurrentCultureIgnoreCase comparison)
'       œil starts with oe: True (InvariantCulture comparison)
'       œil starts with oe: True (InvariantCultureIgnoreCase comparison)
'       œil starts with oe: False (Ordinal comparison)
'       œil starts with oe: False (OrdinalIgnoreCase comparison)
'       
'       læring} starts with lae: True (CurrentCulture comparison)
'       læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
'       læring} starts with lae: True (InvariantCulture comparison)
'       læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
'       læring} starts with lae: False (Ordinal comparison)
'       læring} starts with lae: False (OrdinalIgnoreCase comparison)

설명

메서드는 StartsWith 매개 변수를 value 이 문자열의 시작 부분에 있는 부분 문자열과 비교하고 같은지 여부를 나타내는 값을 반환합니다. 같 value 게 하려면 이 동일한 문자열에 대한 참조이거나 빈 문자열("")이거나 이 문자열의 시작 부분과 일치해야 합니다. 메서드에서 수행하는 StartsWith 비교 유형은 매개 변수의 comparisonType 값에 따라 달라집니다. 비교는 현재 문화권(StringComparison.CurrentCultureStringComparison.CurrentCultureIgnoreCase) 또는 고정 문화권( 및 StringComparison.InvariantCultureIgnoreCase)의 규칙을 사용하거나 코드 포인트(StringComparison.InvariantCultureStringComparison.Ordinal 또는 StringComparison.OrdinalIgnoreCase)의 문자별 비교로 구성됩니다. 비교는 대/소문자 구분(StringComparison.CurrentCulture, StringComparison.InvariantCulture또는 )일 StringComparison.Ordinal수도 있고 대/소문자(, , StringComparison.OrdinalIgnoreCaseStringComparison.InvariantCultureIgnoreCase)를StringComparison.CurrentCultureIgnoreCase 무시할 수도 있습니다.

추가 정보

적용 대상

StartsWith(Char)

이 문자열 인스턴스가 지정한 문자로 시작하는지를 확인합니다.

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

매개 변수

value
Char

비교할 문자입니다.

반환

이 문자열의 시작 부분이 true와 일치하면 value이고, 그러지 않으면 false입니다.

설명

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

적용 대상

StartsWith(String)

이 문자열 인스턴스의 시작 부분과 지정한 문자열이 일치하는지를 확인합니다.

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

매개 변수

value
String

비교할 문자열입니다.

반환

이 문자열의 시작 부분이 true와 일치하면 value이고, 그러지 않으면 false입니다.

예외

valuenull입니다.

예제

다음 예제에서는 메서드를 StripStartTags 사용하여 StartsWith(String) 문자열의 시작 부분에서 HTML 시작 태그를 제거하는 메서드를 정의합니다. 줄의 StripStartTags 시작 부분에 있는 여러 HTML 시작 태그가 제거되도록 메서드를 재귀적으로 호출합니다. 이 예제에서는 문자열에 포함된 HTML 태그를 제거하지 않습니다.

using namespace System;

String^ StripStartTags( String^ item )
{
   // Determine whether a tag begins the string.
   if (item->Trim()->StartsWith("<")) {
      // Find the closing tag.
      int lastLocation = item->IndexOf(">");
      
      // Remove the tag.
      if ( lastLocation >= 0 ) {
         item = item->Substring(lastLocation+ 1);
            
         // Remove any additional starting tags.
         item = StripStartTags(item);
      }      
   }

   return item;
}

int main()
{
   array<String^>^ strSource = { "<b>This is bold text</b>",
                   "<H1>This is large Text</H1>",
                   "<b><i><font color=green>This has multiple tags</font></i></b>",
                   "<b>This has <i>embedded</i> tags.</b>",
                   "<This line simply begins with a lesser than symbol, it should not be modified" };
   
   // Display the initial string array.
   Console::WriteLine("The original strings:");
   Console::WriteLine("---------------------");
   for each (String^ s in strSource)    
      Console::WriteLine( s );
   Console::WriteLine();

   Console::WriteLine( "Strings after starting tags have been stripped:");
   Console::WriteLine( "-----------------------------------------------");
   
   // Display the strings with starting tags removed.
   for each (String^ s in strSource)
      Console::WriteLine(StripStartTags(s));
}
// The example displays the following output:
//    The original strings:
//    ---------------------
//    <b>This is bold text</b>
//    <H1>This is large Text</H1>
//    <b><i><font color = green>This has multiple tags</font></i></b>
//    <b>This has <i>embedded</i> tags.</b>
//    <This line simply begins with a lesser than symbol, it should not be modified
//    
//    Strings after starting tags have been stripped:
//    -----------------------------------------------
//    This is bold text</b>
//    This is large Text</H1>
//    This has multiple tags</font></i></b>
//    This has <i>embedded</i> tags.</b>
//    <This line simply begins with a lesser than symbol, it should not be modified
using System;

public class Example
{
   public static void Main() {
      string [] strSource = { "<b>This is bold text</b>", "<H1>This is large Text</H1>",
                "<b><i><font color=green>This has multiple tags</font></i></b>",
                "<b>This has <i>embedded</i> tags.</b>",
                "<This line simply begins with a lesser than symbol, it should not be modified" };

      // Display the initial string array.
      Console.WriteLine("The original strings:");
      Console.WriteLine("---------------------");
      foreach (var s in strSource)
         Console.WriteLine(s);
      Console.WriteLine();

      Console.WriteLine("Strings after starting tags have been stripped:");
      Console.WriteLine("-----------------------------------------------");

      // Display the strings with starting tags removed.
     foreach (var s in strSource)
        Console.WriteLine(StripStartTags(s));
   }

   private static string StripStartTags(string item)
   {
      // Determine whether a tag begins the string.
      if (item.Trim().StartsWith("<")) {
         // Find the closing tag.
         int lastLocation = item.IndexOf( ">" );
         // Remove the tag.
         if (lastLocation >= 0) {
            item =  item.Substring( lastLocation + 1 );

            // Remove any additional starting tags.
            item = StripStartTags(item);
         }
      }

      return item;
   }
}
// The example displays the following output:
//    The original strings:
//    ---------------------
//    <b>This is bold text</b>
//    <H1>This is large Text</H1>
//    <b><i><font color = green>This has multiple tags</font></i></b>
//    <b>This has <i>embedded</i> tags.</b>
//    <This line simply begins with a lesser than symbol, it should not be modified
//
//    Strings after starting tags have been stripped:
//    -----------------------------------------------
//    This is bold text</b>
//    This is large Text</H1>
//    This has multiple tags</font></i></b>
//    This has <i>embedded</i> tags.</b>
//    <This line simply begins with a lesser than symbol, it should not be modified
let rec stripStartTags (item: string) =
    // Determine whether a tag begins the string.
    if item.Trim().StartsWith "<" then
        // Find the closing tag.
        let lastLocation = item.IndexOf ">"
        // Remove the tag.
        let item = 
            if lastLocation >= 0 then
                item.Substring( lastLocation + 1 )
            else 
                item

        // Remove any additional starting tags.
        stripStartTags item
    else
        item

let strSource = 
    [| "<b>This is bold text</b>"; "<H1>This is large Text</H1>"
       "<b><i><font color=green>This has multiple tags</font></i></b>"
       "<b>This has <i>embedded</i> tags.</b>"
       "<This line simply begins with a lesser than symbol, it should not be modified" |]

// Display the initial string array.
printfn "The original strings:"
printfn "---------------------"
for s in strSource do
    printfn $"{s}"
printfn ""

printfn "Strings after starting tags have been stripped:"
printfn "-----------------------------------------------"

// Display the strings with starting tags removed.
for s in strSource do
    printfn $"{stripStartTags s}"

// The example displays the following output:
//    The original strings:
//    ---------------------
//    <b>This is bold text</b>
//    <H1>This is large Text</H1>
//    <b><i><font color = green>This has multiple tags</font></i></b>
//    <b>This has <i>embedded</i> tags.</b>
//    <This line simply begins with a lesser than symbol, it should not be modified
//
//    Strings after starting tags have been stripped:
//    -----------------------------------------------
//    This is bold text</b>
//    This is large Text</H1>
//    This has multiple tags</font></i></b>
//    This has <i>embedded</i> tags.</b>
//    <This line simply begins with a lesser than symbol, it should not be modified
Public Class Example
   Public Shared Sub Main()
      Dim strSource() As String = { "<b>This is bold text</b>", 
                         "<H1>This is large Text</H1>", 
                         "<b><i><font color = green>This has multiple tags</font></i></b>", 
                         "<b>This has <i>embedded</i> tags.</b>", 
                         "<This line simply begins with a lesser than symbol, it should not be modified" }

      ' Display the initial string array.
      Console.WriteLine("The original strings:")
      Console.WriteLine("---------------------")
      For Each s In strSource
         Console.WriteLine(s)
      Next 
      Console.WriteLine()

      Console.WriteLine("Strings after starting tags have been stripped:")
      Console.WriteLine("-----------------------------------------------") 

      ' Display the strings with starting tags removed.
      For Each s In strSource
         Console.WriteLine(StripStartTags(s))
      Next 
   End Sub 

   Private Shared Function StripStartTags(item As String) As String
      ' Determine whether a tag begins the string.
      If item.Trim().StartsWith("<") Then
         ' Find the closing tag.
         Dim lastLocation As Integer = item.IndexOf(">")
         If lastLocation >= 0 Then
            ' Remove the tag.
            item = item.Substring((lastLocation + 1))
            
            ' Remove any additional starting tags.
            item = StripStartTags(item)
         End If
      End If
      
      Return item
   End Function 
End Class 
' The example displays the following output:
'    The original strings:
'    ---------------------
'    <b>This is bold text</b>
'    <H1>This is large Text</H1>
'    <b><i><font color = green>This has multiple tags</font></i></b>
'    <b>This has <i>embedded</i> tags.</b>
'    <This line simply begins with a lesser than symbol, it should not be modified
'    
'    Strings after starting tags have been stripped:
'    -----------------------------------------------
'    This is bold text</b>
'    This is large Text</H1>
'    This has multiple tags</font></i></b>
'    This has <i>embedded</i> tags.</b>
'    <This line simply begins with a lesser than symbol, it should not be modified

설명

이 메서드는 value 와 길이value가 같은 이 instance 시작 부분 문자열과 비교하고 같은지 여부를 나타내는 표시를 반환합니다. 같 value 게 하려면 빈 문자열()String.Empty이어야 합니다. 이 instance 대한 참조이거나 이 instance 시작 부분과 일치해야 합니다.

이 메서드는 현재 문화권을 사용하여 단어(대/소문자 구분 및 문화권 구분) 비교를 수행합니다.

호출자 참고

문자열 사용에 대한 모범 사례에 설명된 대로 기본값을 대체하는 문자열 비교 메서드를 호출하지 말고 매개 변수를 명시적으로 지정해야 하는 메서드를 호출하는 것이 좋습니다. 문자열이 현재 문화권의 문자열 비교 규칙을 사용하여 특정 부분 문자열로 시작하는지 여부를 확인하려면 메서드 오버로드를 해당 매개 변수의 값 CurrentCulturecomparisonType 으로 호출 StartsWith(String, StringComparison) 하여 명시적으로 의도를 알릴 수 있습니다. 언어 인식 비교가 필요하지 않은 경우 를 사용하는 Ordinal것이 좋습니다.

추가 정보

적용 대상