String.StartsWith String.StartsWith String.StartsWith String.StartsWith Method

定義

この文字列インスタンスの先頭が、指定した文字列と一致するかどうかを判断します。Determines whether the beginning of this string instance matches a specified string.

オーバーロード

StartsWith(String, StringComparison) StartsWith(String, StringComparison) StartsWith(String, StringComparison) StartsWith(String, StringComparison)

指定された比較オプションを使って比較した場合に、この文字列インスタンスの先頭が、指定された文字列と一致するかどうかを判断します。Determines whether the beginning of this string instance matches the specified string when compared using the specified comparison option.

StartsWith(String, Boolean, CultureInfo) StartsWith(String, Boolean, CultureInfo) StartsWith(String, Boolean, CultureInfo) StartsWith(String, Boolean, CultureInfo)

指定されたカルチャを使って比較した場合に、この文字列インスタンスの先頭が、指定された文字列と一致するかどうかを判断します。Determines whether the beginning of this string instance matches the specified string when compared using the specified culture.

StartsWith(String) StartsWith(String) StartsWith(String) StartsWith(String)

この文字列インスタンスの先頭が、指定した文字列と一致するかどうかを判断します。Determines whether the beginning of this string instance matches the specified string.

StartsWith(Char) StartsWith(Char) StartsWith(Char) StartsWith(Char)

StartsWith(String, StringComparison) StartsWith(String, StringComparison) StartsWith(String, StringComparison) StartsWith(String, StringComparison)

指定された比較オプションを使って比較した場合に、この文字列インスタンスの先頭が、指定された文字列と一致するかどうかを判断します。Determines whether the beginning of this string instance matches the specified string when compared using the specified comparison option.

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

パラメーター

value
String String String String

比較対象の文字列。The string to compare.

comparisonType
StringComparison StringComparison StringComparison StringComparison

この文字列と value との比較方法を決定する列挙値の 1 つ。One of the enumeration values that determines how this string and value are compared.

戻り値

インスタンスが true で開始する場合は value。それ以外の場合は falsetrue if this instance begins with value; otherwise, false.

例外

comparisonTypeStringComparison 値ではありません。comparisonType is not a StringComparison value.

次の例では、文字列を検索する単語で始まる長い文字列の先頭に「、」「、」。The following example searches for the string "the" at the beginning of a longer string that begins with the word "The". 例への呼び出しからの出力として、StartsWith(String, StringComparison)カルチャに依存しないが、大文字の比較を実行するメソッドは、文字列を文字列に一致するカルチャおよび大文字小文字 insensitive の比較を実行するための呼び出し中に一致するように失敗します。As the output from the example shows, a call to the StartsWith(String, StringComparison) method that performs a culture-insensitive but case-sensitive comparison fails to match the string, while a call that performs a culture- and case-insensitive comparison matches the string.

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
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

次の例では、文字列が特定の部分文字列で始まるかどうかを判断します。The following example determines whether a string starts with a particular substring. これには、2 次元の文字列配列を初期化します。It initializes a two-dimensional string array. 2 番目の次元の最初の要素には、文字列が含まれていて、2 番目の要素には、最初の文字列の先頭で検索する文字列が含まれています。The first element in the second dimension contains a string, and the second element contains the string to search for at the start of the first string. 結果は、カルチャ、ケースが無視されるかどうか、および序数の比較を実行するかどうかの選択肢の影響を受けます。The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed. 文字列のインスタンスにする合字が含まれている場合、連続する文字を含むカルチャに依存した比較が正常に一致することを注意してください。Note that when the string instance contains a ligature, culture-sensitive comparisons with its consecutive characters successfully match.

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)
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この文字列の先頭の部分文字列のパラメーターが等しいかどうかを示す値を返します。The StartsWith method compares the value parameter to the substring at the beginning of this string and returns a value that indicates whether they are equal. 等しく、valueこの同じ文字列への参照である必要があります、空の文字列にする必要があります ("")、またはこの文字列の先頭に一致する必要があります。To be equal, value must be a reference to this same string, must be the empty string (""), or must match the beginning of this string. によって実行される比較の種類、StartsWithメソッドの値によって異なります、comparisonTypeパラメーター。The type of comparison performed by the StartsWith method depends on the value of the comparisonType parameter. 比較は、現在のカルチャの規則を使用できます (StringComparison.CurrentCultureStringComparison.CurrentCultureIgnoreCase) またはインバリアント カルチャ (StringComparison.InvariantCultureStringComparison.InvariantCultureIgnoreCase)、またはコード ポイントの文字の比較で構成されていることができます (StringComparison.OrdinalまたはStringComparison.OrdinalIgnoreCase).The comparison can use the conventions of the current culture (StringComparison.CurrentCulture and StringComparison.CurrentCultureIgnoreCase) or the invariant culture (StringComparison.InvariantCulture and StringComparison.InvariantCultureIgnoreCase), or it can consist of a character-by-character comparison of code points (StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase). この比較は大文字小文字を区別も指定できます (StringComparison.CurrentCultureStringComparison.InvariantCulture、またはStringComparison.Ordinal)、または大文字小文字を区別できます (StringComparison.CurrentCultureIgnoreCaseStringComparison.InvariantCultureIgnoreCaseStringComparison.OrdinalIgnoreCase)。The comparison can also be case-sensitive (StringComparison.CurrentCulture, StringComparison.InvariantCulture, or StringComparison.Ordinal), or it can ignore case (StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase, StringComparison.OrdinalIgnoreCase).

こちらもご覧ください

StartsWith(String, Boolean, CultureInfo) StartsWith(String, Boolean, CultureInfo) StartsWith(String, Boolean, CultureInfo) StartsWith(String, Boolean, CultureInfo)

指定されたカルチャを使って比較した場合に、この文字列インスタンスの先頭が、指定された文字列と一致するかどうかを判断します。Determines whether the beginning of this string instance matches the specified string when compared using the specified culture.

public:
 bool StartsWith(System::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 String String String

比較対象の文字列。The string to compare.

ignoreCase
Boolean Boolean Boolean Boolean

比較時に大文字と小文字の区別を無視する場合は true。それ以外の場合は falsetrue to ignore case during the comparison; otherwise, false.

culture
CultureInfo CultureInfo CultureInfo CultureInfo

この文字列と value との比較方法を決定するカルチャ情報。Cultural information that determines how this string and value are compared. culturenull の場合は、現在のカルチャが使用されます。If culture is null, the current culture is used.

戻り値

true パラメーターがこの文字列の先頭と一致する場合は value。それ以外の場合は falsetrue if the value parameter matches the beginning of this string; otherwise, false.

例外

次の例では、別の文字列の先頭に文字列が発生したかどうかを判断します。The following example determines whether a string occurs at the beginning of another string. StartsWith大文字小文字の区別、大文字小文字の区別、および検索の結果に影響する異なるカルチャを使用していくつかのメソッドが呼び出されます。The StartsWith method is called several times using case sensitivity, case insensitivity, and different cultures that influence the results of the search.

// 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.

Imports System
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 'Main
End Class 'Sample

'
'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、し、それらが等しいかどうかを示す値を返します。This method compares the value parameter to the substring at the beginning of this string that is the same length as value, and returns a value that indicates whether they are equal. 等しいものvalue空の文字列を指定する必要があります (String.Empty)、この同じインスタンスへの参照があります。 または、このインスタンスの先頭に一致する必要があります。To be equal, value must be an empty string (String.Empty), must be a reference to this same instance, or must match the beginning of this instance.

このメソッドは、指定した大文字小文字の区別とカルチャを使用して、比較を実行します。This method performs a comparison using the specified casing and culture.

StartsWith(String) StartsWith(String) StartsWith(String) StartsWith(String)

この文字列インスタンスの先頭が、指定した文字列と一致するかどうかを判断します。Determines whether the beginning of this string instance matches the specified 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 String String String

比較対象の文字列。The string to compare.

戻り値

この文字列の先頭が value と一致する場合は true。それ以外の場合は falsetrue if value matches the beginning of this string; otherwise, false.

例外

次の例では、定義、StripStartTagsメソッドを使用する、 StartsWith(String) HTML を削除する方法は、文字列の先頭からタグを起動します。The following example defines a StripStartTags method that uses the StartsWith(String) method to remove HTML start tags from the beginning of a string. なお、StripStartTagsメソッドには、行の先頭に複数の HTML 開始タグが削除されるようにするには、再帰的には呼び出されます。Note that the StripStartTags method is called recursively to ensure that multiple HTML start tags at the beginning of the line are removed. 例では、文字列に埋め込まれた HTML タグは削除されません。The example does not remove HTML tags embedded in a string.

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
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、それらが等しいかどうか示す値を返します。This method compares value to the substring at the beginning of this instance that is the same length as value, and returns an indication whether they are equal. 等しいものvalue空の文字列を指定する必要があります (String.Empty)、この同じインスタンスへの参照があります。 または、このインスタンスの先頭に一致する必要があります。To be equal, value must be an empty string (String.Empty), must be a reference to this same instance, or must match the beginning of this instance.

このメソッドは、現在のカルチャを使用して、単語 (大文字小文字を区別し、カルチャに依存) 比較を実行します。This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture.

注意 (呼び出し元)

説明したよう文字列を使用するためのベスト プラクティス既定値を代入しを明示的に指定するパラメーターを必要とするメソッドを呼び出す代わりに文字列比較メソッドを呼び出さないようにすることをお勧めします。As explained in Best Practices for Using Strings, we recommend that you avoid calling string comparison methods that substitute default values and instead call methods that require parameters to be explicitly specified. 文字列が現在のカルチャの文字列比較の規則を使用して、特定の部分文字列で始まるかどうかを確認するのには、呼び出し、StartsWith(String, StringComparison)メソッドのオーバー ロードの値を持つCurrentCulturecomparisonTypeパラメーター。To determine whether a string begins with a particular substring by using the string comparison rules of the current culture, call the StartsWith(String, StringComparison) method overload with a value of CurrentCulture for its comparisonType parameter.

こちらもご覧ください

StartsWith(Char) StartsWith(Char) StartsWith(Char) 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 Char Char Char

戻り値

適用対象