Share via


String.EndsWith メソッド

このインスタンスの末尾が、指定した String と一致するかどうかを判断します。

Public Function EndsWith( _
   ByVal value As String _) As Boolean
[C#]
public bool EndsWith(stringvalue);
[C++]
public: bool EndsWith(String* value);
[JScript]
public function EndsWith(
   value : String) : Boolean;

パラメータ

戻り値

このインスタンスの末尾が value に一致する場合は true 。それ以外の場合は false

例外

例外の種類 条件
ArgumentNullException value が null 参照 (Visual Basic では Nothing) です。

解説

このメソッドは、 value. Length と、現在のインスタンスの末尾にある、 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

[C#] 
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;
    }
}

[C++] 
#using <mscorlib.dll>

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(S">")) {

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

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

   String* strSource[] = { S"<b>This is bold text</b>", S"<H1>This is large Text</H1>",
      S"<b><i><font color=green>This has multiple tags</font></i></b>",
      S"<b>This has <i>embedded</i> tags.</b>",
      S"This line simply ends with a greater than symbol, it should not be modified>" };

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

   // print out the initial array of strings
   IEnumerator* myEnum1 = strSource->GetEnumerator();
   while (myEnum1->MoveNext()) {
      String* s = __try_cast<String*>(myEnum1->Current);

      Console::WriteLine(s);
   }
   Console::WriteLine();

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

   // print out the array of strings
   IEnumerator* myEnum2 = strSource->GetEnumerator();
   while (myEnum2->MoveNext()) {
      String* s = __try_cast<String*>(myEnum2->Current);

      Console::WriteLine(StripEndTags(s));
   }
}

[JScript] 
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();

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | StartsWith | Compare