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

String.EndsWith メソッド (String)

このインスタンス末尾が、指定した文字列一致するかどうか判断します

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

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

解説解説

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

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

使用例使用例

EndsWith メソッド使用する方法次のコード例示します

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 ends with a greater than
 symbol, it should not be modified>"}

        ' process an input file that contains html tags.
        ' this sample checks for multiple tags at the end of the line,
 rather than simply
        ' removing the last one.
        ' note: HTML markup tags always end in a greater than symbol
 (>).


        Console.WriteLine("The following lists the items before
 the ends 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 ends have been stripped:")
        Console.WriteLine("----------------------------------------------------------------")

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

    End Sub 'Main

    Private Shared Function
 StripEndTags(item As String) As
 String

        ' try to find a tag at the end of the line using EndsWith
        If item.Trim().EndsWith(">")
 Then

            ' now search for the opening tag...
            Dim lastLocation As Integer
 = item.LastIndexOf("</")
            If lastLocation >= 0 Then

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

    Return Item
    End Function 'StripEndTags

End Class 'EndsWithTest
using System;

public class EndsWithTest {
    public static void Main()
 {

        // process an input file that contains html tags.
        // this sample checks for multiple tags at the end of the line,
 rather than simply
        // removing the last one.
        // note: HTML markup tags always end in a greater than symbol
 (>).

        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 ends with a greater 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
        foreach ( string s in
 strSource )
            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
        foreach ( string s in
 strSource )
            Console.WriteLine( StripEndTags( s ) );
    }

    private static string
 StripEndTags( string item ) {

            // try to find a tag at the end of the line using EndsWith
            if (item.Trim().EndsWith(">")) {

                // now search for the opening tag...
                int lastLocation = item.LastIndexOf( "</"
 );

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

    return item;
    }
}
using namespace System;
using namespace System::Collections;
String^ StripEndTags( String^ item )
{
   
   // try to find a tag at the end of the line using EndsWith
   if ( item->Trim()->EndsWith( ">" ) )
   {
      
      // now search for the opening tag...
      int lastLocation = item->LastIndexOf( "</"
 );
      
      // remove the identified section, if it is a valid region
      if ( lastLocation >= 0 )
            item = item->Substring( 0, lastLocation );
   }

   return item;
}

int main()
{
   
   // process an input file that contains html tags.
   // this sample checks for multiple tags at the end of the line, rather
 than simply
   // removing the last one.
   // note: HTML markup tags always end in a greater than symbol (>).
   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 ends with a greater 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
   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 ends 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( StripEndTags( s ) );
   }
}

import System.*;

public class EndsWithTest
{
    public static void main(String[]
 args)
    {
        // process an input file that contains html tags.
        // this sample checks for multiple tags at the end of the line,
 
        // rather than simply removing the last one.
        // note: HTML markup tags always end in a greater than symbol
 (>).
        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 ends with a greater 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 = (String)strSource.get_Item(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 = (String)strSource.get_Item(iCtr);
            Console.WriteLine(StripEndTags(s));
        }
    } //main

    private static String StripEndTags(String
 item)
    {
        // try to find a tag at the end of the line using EndsWith
        if (item.Trim().EndsWith(">")) {
            // now search for the opening tag...
            int lastLocation = item.LastIndexOf("</");
            // remove the identified section, if it is a valid region
            if (lastLocation >= 0) {
                item = item.Substring(0, lastLocation);
            }
        }
        return item;
    } //StripEndTags
} //EndsWithTest
import System;

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

        // process an input file that contains html tags.
        // this sample checks for multiple tags at the end of the line,
 rather than simply
        // removing the last one.
        // note: HTML markup tags always end in a greater than symbol
 (>).

        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 ends with a greater 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( var i : int
 in strSource )
            Console.WriteLine( strSource[i] );

        Console.WriteLine();
        Console.WriteLine("The following lists the items after the ends have
 been stripped:");
        Console.WriteLine("----------------------------------------------------------------");
        // print out the array of strings
        for( i in strSource )
            Console.WriteLine( StripEndTags( strSource[i] ) );
    }

    private static function
 StripEndTags( item : String ) : String {
        // try to find a tag at the end of the line using EndsWith
        if (item.Trim().EndsWith(">")) {
            // now search for the opening tag...
            var lastLocation : int = item.LastIndexOf(
 "</" );
            // remove the identified section, if it is a valid region
            if ( lastLocation >= 0 )
                item =  item.Substring( 0, lastLocation );
        }
        return item;
    }
}
EndsWithTest.Main();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

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

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

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

<ComVisibleAttribute(False)> _
Public Function EndsWith ( _
    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.EndsWith(value, comparisonType)
[ComVisibleAttribute(false)] 
public bool EndsWith (
    string value,
    StringComparison comparisonType
)
[ComVisibleAttribute(false)] 
public:
bool EndsWith (
    String^ value, 
    StringComparison comparisonType
)
/** @attribute ComVisibleAttribute(false) */ 
public boolean EndsWith (
    String value, 
    StringComparison comparisonType
)
ComVisibleAttribute(false) 
public function EndsWith (
    value : String, 
    comparisonType : StringComparison
) : boolean

パラメータ

value

比較する String オブジェクト

comparisonType

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

戻り値
value パラメータがこの文字列末尾一致する場合trueそれ以外場合false

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

ArgumentException

comparisonTypeStringComparison 値ではありません。

解説解説

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

使用例使用例

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

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

Imports System
Imports System.Threading

Class Sample
    Public Shared Sub Main()
 
        Dim intro As String
 = "Determine whether a string ends 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", "XYZ",
 sc)
            Test("abcXYZ", "xyz",
 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 end"
        '
        If x.EndsWith(y, comparison) Then
            result = "ends"
        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 ends with another string, using
'  different values of StringComparison.
'The current culture is en-US.
'
'StringComparison.CurrentCulture:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'
'StringComparison.CurrentCultureIgnoreCase:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
'
'StringComparison.InvariantCulture:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'
'StringComparison.InvariantCultureIgnoreCase:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
'
'StringComparison.Ordinal:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'
'StringComparison.OrdinalIgnoreCase:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
'
// This example demonstrates the 
// System.String.EndsWith(String, StringComparison) method.

using System;
using System.Threading;

class Sample 
{
    public static void Main()
 
    {
    string intro = "Determine whether a string
 ends 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", "XYZ", sc);
        Test("abcXYZ", "xyz", sc);
        Console.WriteLine();
        }
    }

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

/*
This code example produces the following results:

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

StringComparison.CurrentCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.CurrentCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.InvariantCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.InvariantCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.Ordinal:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.OrdinalIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

*/
// This example demonstrates the 
// System.String.EndsWith(String, StringComparison) method.

using namespace System;
using namespace System::Threading;

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

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

int main()
{
    String^ introMessage =
        "Determine whether a string ends 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", "XYZ", stringCmp);
        Test("abcXYZ", "xyz", stringCmp);
        Console::WriteLine();
    }
}

/*
This code example produces the following results:

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

StringComparison.CurrentCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.CurrentCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.InvariantCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.InvariantCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.Ordinal:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.OrdinalIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

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

String.EndsWith メソッド


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

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

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

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

Public Function EndsWith ( _
    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.EndsWith(value, ignoreCase, culture)
public bool EndsWith (
    string value,
    bool ignoreCase,
    CultureInfo culture
)
public:
bool EndsWith (
    String^ value, 
    bool ignoreCase, 
    CultureInfo^ culture
)
public boolean EndsWith (
    String value, 
    boolean ignoreCase, 
    CultureInfo culture
)
public function EndsWith (
    value : String, 
    ignoreCase : boolean, 
    culture : CultureInfo
) : boolean

パラメータ

value

比較する String オブジェクト

ignoreCase

このインスタンスvalue とを比較する際、大文字と小文字違い無視する場合trueそれ以外場合false

culture

このインスタンスvalue との比較方法決定するカルチャ情報culturenull 参照 (Visual Basic では Nothing) の場合は、現在のカルチャが使用されます。

戻り値
value パラメータがこの文字列末尾一致する場合trueそれ以外場合false

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

解説解説

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

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

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


このページでは「.NET Framework クラス ライブラリ リファレンス」からString.EndsWithを検索した結果を表示しています。
Weblioに収録されているすべての辞書からString.EndsWithを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からString.EndsWith を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「String.EndsWith」の関連用語

String.EndsWithのお隣キーワード
検索ランキング

   

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



String.EndsWithのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS