String.StartsWithとは? わかりやすく解説

String.StartsWith メソッド (String, Boolean, CultureInfo)

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

指定されたカルチャを使って比較した場合に、この文字列の先頭が、指定された文字列と一致するかどうかを判断します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function StartsWith ( _
    value As String, _
    ignoreCase As Boolean, _
    culture As CultureInfo _
) As Boolean
Dim instance As String
Dim value As String
Dim ignoreCase As Boolean
Dim culture As CultureInfo
Dim returnValue As Boolean

returnValue = instance.StartsWith(value, ignoreCase, culture)
public bool StartsWith (
    string value,
    bool ignoreCase,
    CultureInfo culture
)
public:
bool StartsWith (
    String^ value, 
    bool ignoreCase, 
    CultureInfo^ culture
)
public boolean StartsWith (
    String value, 
    boolean ignoreCase, 
    CultureInfo culture
)
public function StartsWith (
    value : String, 
    ignoreCase : boolean, 
    culture : CultureInfo
) : boolean

パラメータ

value

比較対象の String オブジェクト。

ignoreCase

この文字列と value とを比較する際、大文字と小文字の違いを無視する場合は true。それ以外の場合は false。

culture

この文字列と value との比較方法を決定するカルチャ情報。culture が null 参照 (Visual Basic では Nothing) の場合は、現在のカルチャが使用されます。

戻り値
value パラメータがこの文字列の先頭と一致する場合は true。それ以外の場合は false。

例外例外
例外の種類条件

ArgumentNullException

value が null 参照 (Visual Basic では Nothing) です。

解説解説

このメソッドは、value パラメータと、この文字列の先頭にある、value とまったく同じ長さの部分文字列とを比較し、両者が等しいかどうかを示す値を返します。等しいという結果を得るためには、value が、空の文字列 (Empty) であるか、この同じインスタンスへの参照であるか、または、このインスタンスの先頭と一致している必要があります。

このメソッドは、大文字と小文字の区別とカルチャの種類を引数として受け取り、カルチャに依存する単語ベースの比較を実行します。

使用例使用例
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.StartsWith メソッド (String)

このインスタンスの先頭が、指定した文字列と一致するかどうかを判断します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

value が null 参照 (Visual Basic では Nothing) です。

解説解説

このメソッドは、value と、このインスタンスの先頭にある、value とまったく同じ長さの部分文字列とを比較し、両者が等しいかどうかを調べます。等しいという結果を得るためには、value が、空の文字列 (Empty) であるか、この同じインスタンスへの参照であるか、または、このインスタンスの先頭と一致している必要があります。

このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 比較を実行します。

使用例使用例

StartsWith メソッドを使用する方法を次のコード例に示します。

Imports System

Public Class EndsWithTest

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

        ' process a string that contains html tags
        ' this sample does not remove embedded tags (tags in the middle
 of a line)

        Console.WriteLine("The following lists the items before
 the tags have been stripped:")
        Console.WriteLine("-----------------------------------------------------------------")

        ' print out the initial array of strings
        Dim s As String
        For Each s In  strSource
            Console.WriteLine(s)
        Next s

        Console.WriteLine()

        Console.WriteLine("The following lists the items after
 the tags have been stripped:")
        Console.WriteLine("----------------------------------------------------------------")

        ' print out the array of strings
        For Each s In  strSource
            Console.WriteLine(StripStartTags(s))
        Next s

    End Sub 'Main

    Private Shared Function
 StripStartTags(item As String) As
 String

        ' try to find a tag at the start of the line using StartsWith
        If item.Trim().StartsWith("<")
 Then

            ' now search for the closing tag...
           Dim lastLocation As Integer
 = item.IndexOf(">")

            If lastLocation >= 0 Then
                ' remove the identified section, if it is a valid region
                item = item.Substring((lastLocation + 1))
            End If
        End If

        Return item

    End Function 'StripStartTags

End Class 'EndsWithTest
using System;

public class EndsWithTest {
    public static void Main()
 {

        // process a string that contains html tags
        // this sample does not remove embedded tags (tags in the middle
 of a line)

        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" };

        Console.WriteLine("The following lists the items before the tags have
 been stripped:");
        Console.WriteLine("-----------------------------------------------------------------");

        // print out the initial array of strings
        foreach ( string s in
 strSource )
            Console.WriteLine( s );

        Console.WriteLine();

        Console.WriteLine("The following lists the items after the tags have
 been stripped:");
        Console.WriteLine("----------------------------------------------------------------");

        // print out the array of strings
        foreach ( string s in
 strSource )
            Console.WriteLine( StripStartTags( s ) );
    }

    private static string
 StripStartTags( string item ) {

        // try to find a tag at the start of the line using StartsWith
        if (item.Trim().StartsWith("<")) {

            // now search for the closing tag...
            int lastLocation = item.IndexOf( ">"
 );

            // remove the identified section, if it is a valid region
            if ( lastLocation >= 0 )
                item =  item.Substring( lastLocation + 1 );
        }

        return item;
    }
}
using namespace System;
using namespace System::Collections;
String^ StripStartTags( String^ item )
{
   
   // try to find a tag at the start of the line using StartsWith
   if ( item->Trim()->StartsWith( "<" ) )
   {
      
      // now search for the closing tag->->.
      int lastLocation = item->IndexOf( ">" );
      
      // remove the identified section, if it is a valid region
      if ( lastLocation >= 0 )
            item = item->Substring( lastLocation + 1 );
   }

   return item;
}

int main()
{
   
   // process a string that contains html tags
   // this sample does not remove embedded tags (tags in the middle
 of a line)
   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"};
   Console::WriteLine( "The following lists the items before the tags have been
 stripped:" );
   Console::WriteLine( "-----------------------------------------------------------------"
 );
   
   // print out the initial array of strings
   IEnumerator^ myEnum1 = strSource->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      String^ s = safe_cast<String^>(myEnum1->Current);
      Console::WriteLine( s );
   }

   Console::WriteLine();
   Console::WriteLine( "The following lists the items after the tags have been
 stripped:" );
   Console::WriteLine( "----------------------------------------------------------------"
 );
   
   // print [Out] the* array of strings
   IEnumerator^ myEnum2 = strSource->GetEnumerator();
   while ( myEnum2->MoveNext() )
   {
      String^ s = safe_cast<String^>(myEnum2->Current);
      Console::WriteLine( StripStartTags( s ) );
   }
}

import System.*;

public class EndsWithTest
{
    public static void main(String[]
 args)
    {
        // process a string that contains html tags
        // this sample does not remove embedded tags
        // (tags in the middle of a line)
        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" };

        Console.WriteLine("The following lists the items before" 
            + " the ends have been stripped:");
        Console.WriteLine("-------------------------------------" 
            + "----------------------------");
        // print out the initial array of strings
        for (int iCtr = 0; iCtr < strSource.get_Length();
 iCtr++) {
            String s = strSource[iCtr];
            Console.WriteLine(s);
        }
        Console.WriteLine();

        Console.WriteLine("The following lists the items after" 
            + " the ends have been stripped:");
        Console.WriteLine("---------------------------------------" 
            + "-------------------------");
        // print out the array of strings
        for (int iCtr = 0; iCtr < strSource.get_Length();
 iCtr++) {
            String s = strSource[iCtr];
            Console.WriteLine(StripStartTags(s));
        }
    } //main

    private static String StripStartTags(String
 item)
    {
        // try to find a tag at the start of the line using StartsWith
        if (item.Trim().StartsWith("<")) {
            // now search for the closing tag...
            int lastLocation = item.IndexOf(">");
            // remove the identified section, if it is a valid region
            if (lastLocation >= 0) {
                item = item.Substring(lastLocation + 1);
            }
        }
        return item;
    } //StripStartTags
} //EndsWithTest
import System;

public class EndsWithTest {
    public static function
 Main() : void {

        // process a string that contains html tags
        // this sample does not remove embedded tags (tags in the middle
 of a line)

        var strSource : 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" ];

        Console.WriteLine("The following lists the items before the tags have
 been stripped:");
        Console.WriteLine("-----------------------------------------------------------------");

        // print out the initial array of strings
        for( var i : int
 in strSource )
            Console.WriteLine( strSource[i] );

        Console.WriteLine();

        Console.WriteLine("The following lists the items after the tags have
 been stripped:");
        Console.WriteLine("----------------------------------------------------------------");

        // print out the array of strings
        for ( i in strSource )
            Console.WriteLine( StripStartTags( strSource[i] ) );
    }

    private static function
 StripStartTags( item : String ) : String  {

        // try to find a tag at the start of the line using StartsWith
        if (item.Trim().StartsWith("<")) {

            // now search for the closing tag...
            var lastLocation : int = item.IndexOf(
 ">" );

            // remove the identified section, if it is a valid region
            if ( lastLocation >= 0 )
                item =  item.Substring( lastLocation + 1 );
        }

        return item;
    }
}
EndsWithTest.Main();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.StartsWith メソッド (String, StringComparison)

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

指定された比較オプションを使って比較した場合に、この文字列の先頭が、指定された文字列と一致するかどうかを判断します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(False)> _
Public Function StartsWith ( _
    value As String, _
    comparisonType As StringComparison _
) As Boolean
Dim instance As String
Dim value As String
Dim comparisonType As StringComparison
Dim returnValue As Boolean

returnValue = instance.StartsWith(value, comparisonType)
[ComVisibleAttribute(false)] 
public bool StartsWith (
    string value,
    StringComparison comparisonType
)
[ComVisibleAttribute(false)] 
public:
bool StartsWith (
    String^ value, 
    StringComparison comparisonType
)
/** @attribute ComVisibleAttribute(false) */ 
public boolean StartsWith (
    String value, 
    StringComparison comparisonType
)
ComVisibleAttribute(false) 
public function StartsWith (
    value : String, 
    comparisonType : StringComparison
) : boolean

パラメータ

value

比較する String オブジェクト。

comparisonType

この文字列と value との比較方法を決定する、いずれかの StringComparison 値。

戻り値
value パラメータがこの文字列の先頭と一致する場合は true。それ以外の場合は false。

例外例外
例外の種類条件

ArgumentNullException

value が null 参照 (Visual Basic では Nothing) です。

ArgumentException

comparisonType が StringComparison 値ではありません。

解説解説

StartsWith メソッドは、value パラメータと、この文字列の先頭にある部分文字列とを比較し、両者が等しいかどうかを示す値を返します。等しいという結果を得るためには、value が、この同じインスタンスへの参照であるか、空の文字列 ("") であるか、または、この文字列の先頭と一致している必要があります。StartsWith メソッドによって実行される比較の種類は、comparisonType パラメータの値に依存します。

使用例使用例

ある文字列が特定の部分文字列で始まっているかどうかを判断するコード例を次に示します。選択したカルチャ、大文字と小文字の区別、比較の種類 (序数ベースにしたかどうか) が実行結果に影響します。

' This example demonstrates the 
' System.String.StartsWith(String, StringComparison) method.

Imports System
Imports System.Threading

Class Sample
    Public Shared Sub Main()
 
        Dim intro As String
 = "Determine whether a string starts with another string, "
 & _
                              "using" & vbCrLf
 & "  different values of StringComparison."
        
        Dim scValues As StringComparison()
 =  { _
                        StringComparison.CurrentCulture, _
                        StringComparison.CurrentCultureIgnoreCase, _
                        StringComparison.InvariantCulture, _
                        StringComparison.InvariantCultureIgnoreCase, _
                        StringComparison.Ordinal, _
                        StringComparison.OrdinalIgnoreCase }
        '
        Console.Clear()
        Console.WriteLine(intro)
        
        ' Display the current culture because the culture-specific comparisons
        ' can produce different results with different cultures.
        Console.WriteLine("The current culture is {0}."
 & vbCrLf, _
                           Thread.CurrentThread.CurrentCulture.Name)
        ' Determine whether three versions of the letter I are equal
 to each other. 
        Dim sc As StringComparison
        For Each sc In 
 scValues
            Console.WriteLine("StringComparison.{0}:",
 sc)
            Test("ABCxyz", "ABC",
 sc)
            Test("ABCxyz", "abc",
 sc)
            Console.WriteLine()
        Next sc
    
    End Sub 'Main

    Protected Shared Sub
 Test(ByVal x As String,
 ByVal y As String, _
                              ByVal comparison As
 StringComparison) 
        Dim resultFmt As String
 = """{0}"" {1}
 with ""{2}""."
        Dim result As String
 = "does not start"
        '
        If x.StartsWith(y, comparison) Then
            result = "starts"
        End If
        Console.WriteLine(resultFmt, x, result, y)
    
    End Sub 'Test
End Class 'Sample

'
'This code example produces the following results:
'
'Determine whether a string starts with another string, using
'  different values of StringComparison.
'The current culture is en-US.
'
'StringComparison.CurrentCulture:
'"ABCxyz" starts with "ABC".
'"ABCxyz" does not start with "abc".
'
'StringComparison.CurrentCultureIgnoreCase:
'"ABCxyz" starts with "ABC".
'"ABCxyz" starts with "abc".
'
'StringComparison.InvariantCulture:
'"ABCxyz" starts with "ABC".
'"ABCxyz" does not start with "abc".
'
'StringComparison.InvariantCultureIgnoreCase:
'"ABCxyz" starts with "ABC".
'"ABCxyz" starts with "abc".
'
'StringComparison.Ordinal:
'"ABCxyz" starts with "ABC".
'"ABCxyz" does not start with "abc".
'
'StringComparison.OrdinalIgnoreCase:
'"ABCxyz" starts with "ABC".
'"ABCxyz" starts with "abc".
'
// This example demonstrates the 
// System.String.StartsWith(String, StringComparison) method.

using System;
using System.Threading;

class Sample 
{
    public static void Main()
 
    {
    string intro = "Determine whether a string
 starts with another string, " +
                   "using\n  different values of StringComparison.";

    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

//
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because the culture-specific comparisons
// can produce different results with different cultures.
    Console.WriteLine("The current culture is {0}.\n", 
                       Thread.CurrentThread.CurrentCulture.Name);

// Determine whether three versions of the letter I are equal to each
 other. 
    foreach (StringComparison sc in scValues)
        {
        Console.WriteLine("StringComparison.{0}:", sc);
        Test("ABCxyz", "ABC", sc);
        Test("ABCxyz", "abc", sc);
        Console.WriteLine();
        }
    }

    protected static void
 Test(string x, string y, StringComparison
 comparison)
    {
    string resultFmt = "\"{0}\" {1} with \"{2}\".";
    string result = "does not start";
//
    if (x.StartsWith(y, comparison))
        result = "starts";
    Console.WriteLine(resultFmt, x, result, y);
    }
}

/*
This code example produces the following results:

Determine whether a string starts with another string,
 using
  different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.CurrentCultureIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

StringComparison.InvariantCulture:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.InvariantCultureIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

StringComparison.Ordinal:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.OrdinalIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

*/
// This example demonstrates the
// System::String->StartsWith(String, StringComparison) method.

using namespace System;
using namespace System::Threading;

static void Test(String^ testString, String^
 searchString,
    StringComparison comparison)
{
    String^ resultFormat = "\"{0}\" {1} with \"{2}\".";
    String^ resultString = "does not start";

    if (testString->StartsWith(searchString, comparison))
    {
        resultString = "starts";
    }
    Console::WriteLine(resultFormat, testString, resultString, searchString);
}

int main()
{
    String^ introMessage =
        "Determine whether a string starts with another string,
 " +
        "using\ndifferent values of StringComparison.";

    array<StringComparison>^ comparisonValues = {
        StringComparison::CurrentCulture, 
        StringComparison::CurrentCultureIgnoreCase, 
        StringComparison::InvariantCulture, 
        StringComparison::InvariantCultureIgnoreCase,
        StringComparison::Ordinal, StringComparison::OrdinalIgnoreCase};

    Console::Clear();
    Console::WriteLine(introMessage);

    // Display the current culture because the culture-specific comparisons
    // can produce different results with different cultures.
    Console::WriteLine("The current culture is {0}.\n",
        Thread::CurrentThread->CurrentCulture->Name);
    // Perform two tests for each StringComparison
    for each(StringComparison stringCmp in
 comparisonValues)
    {
        Console::WriteLine("StringComparison.{0}:", stringCmp);
        Test("ABCxyz", "ABC", stringCmp);
        Test("ABCxyz", "abc", stringCmp);
        Console::WriteLine();
    }
}

/*
This code example produces the following results:

Determine whether a string starts with another string,
 using
different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.CurrentCultureIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

StringComparison.InvariantCulture:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.InvariantCultureIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

StringComparison.Ordinal:
"ABCxyz" starts with "ABC".
"ABCxyz" does not start with "abc".

StringComparison.OrdinalIgnoreCase:
"ABCxyz" starts with "ABC".
"ABCxyz" starts with "abc".

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.StartsWith メソッド




英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「String.StartsWith」の関連用語

String.StartsWithのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



String.StartsWithのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS