String.LastIndexOf メソッド (String)
指定した String がこのインスタンス内で最後に見つかったインデックス位置をレポートします。
Overloads Public Function LastIndexOf( _
ByVal value As String _) As Integer
[C#]
public int LastIndexOf(stringvalue);
[C++]
public: int LastIndexOf(String* value);
[JScript]
public function LastIndexOf(
value : String) : int;
パラメータ
- value
シークする String 。
戻り値
その文字列が見つかった場合は、 value のインデックス位置。見つからなかった場合は -1。 value が Empty の場合、戻り値は value の最後のインデックス位置です。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | value が null 参照 (Visual Basic では Nothing) です。 |
解説
インデックスの番号付けは 0 から始まります。
このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行します。この検索では、インスタンスの最後の文字位置から開始し、 value が見つかるか、最初の文字位置に到達するまで、インスタンスの先頭へ向かって逆方向に検索を実行します。
使用例
入力文字列の解析中の判定に LastIndexOf メソッドを使用する方法については、次のアプリケーション例を参照してください。
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 名前空間 | String.LastIndexOf オーバーロードの一覧 | Int32 | IndexOf | IndexOfAny | LastIndexOfAny