この記事は機械翻訳されています。英語版の記事を表示するには、[英語] のチェック ボックスをオンにしてください。また、テキストにマウス ポインターを合わせると、ポップアップ ウィンドウに英語のテキストを表示することもできます。
翻訳
英語

String.LastIndexOf メソッド (String, Int32, Int32, StringComparison)

 

指定された文字列がこのインスタンス内で最後に見つかった 0 から始まるインデックス位置をレポートします。 検索は指定された文字位置から開始し、文字列の開始に向かって後方に移動し、文字位置の指定された数だけ行われます。 指定した文字列を検索するときに実行する比較の種類をパラメーターで指定します。

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

public int LastIndexOf(
	string value,
	int startIndex,
	int count,
	StringComparison comparisonType
)

パラメーター

value
Type: System.String

シークする文字列。

startIndex
Type: System.Int32

検索が開始される位置。 検索は、このインスタンスの先頭に向かって startIndex から開始されます。

count
Type: System.Int32

検査する文字位置の数。

comparisonType
Type: System.StringComparison

検索の規則を指定する列挙値の 1 つ。

戻り値

Type: System.Int32

その文字列が見つかった場合は、value パラメーターの 0 から始まる開始インデックス位置。見つからなかった場合、または現在のインスタンスが String.Empty と等しい場合は -1。 valueString.Empty の場合、戻り値は startIndex かこのインスタンスの最後のインデックス位置のいずれか小さい方です。

Exception Condition
ArgumentNullException

valuenull です。

ArgumentOutOfRangeException

count が負の値です。

または

現在のインスタンスが String.Empty と等しくなく、startIndex が負の値です。

または

現在のインスタンスが String.Empty と等しくなく、startIndex がこのインスタンスの長さより大きいです。

または

現在のインスタンスが String.Empty と等しくなく、startIndex + 1 - count した値がこのインスタンス内にはない位置を指定しています。

または

現在のインスタンスが String.Empty と等しく、start が -1 より小さいか 0 より大きいです。

または

現在のインスタンスが String.Empty と等しく、count が 1 より大きいです。

ArgumentException

comparisonType が有効な System.StringComparison 値ではありません。

Index numbering starts from zero. That is, the first character in the string is at index zero and the last is at P:System.String.Length - 1.

The search begins at the startIndex character position and proceeds backward until either value is found or count character positions have been examined. For example, if startIndex is P:System.String.Length - 1, the method searches backward count characters from the last character in the string.

The comparisonType parameter specifies to search for the value parameter using the current or invariant culture, using a case-sensitive or case-insensitive search, and using word or ordinal comparison rules.

呼び出し時の注意:

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search (that is, if comparisonType is not F:System.StringComparison.Ordinal or F:System.StringComparison.OrdinalIgnoreCase), if value contains an ignorable character, the result is equivalent to searching with that character removed. If value consists only of one or more ignorable characters, the M:System.String.LastIndexOf(System.String,System.Int32,System.Int32,System.StringComparison) method always returns startIndex, which is the character position at which the search begins.

In the following example, the M:System.String.LastIndexOf(System.String,System.Int32,System.Int32,System.StringComparison) method is used to find the position of a soft hyphen (U+00AD) followed by an "m" in all but the first character position before the final "m" in two strings. Only one of the strings contains the required substring. If the example is run on the netfx40_short or later, in both cases, because the soft hyphen is an ignorable character, the method returns the index of "m" in the string when it performs a culture-sensitive comparison. When it performs an ordinal comparison, however, it finds the substring only in the first string. Note that in the case of the first string, which includes the soft hyphen followed by an "m", the method fails to return the index of the soft hyphen but instead returns the index of the "m" when it performs a culture-sensitive comparison. The method returns the index of the soft hyphen in the first string only when it performs an ordinal comparison.

using System;

public class Example
{
   public static void Main()
   {
      string searchString = "\u00ADm";
      string s1 = "ani\u00ADmal" ;
      string s2 = "animal";
      int position;

      position = s1.LastIndexOf('m');
      if (position >= 1) {
         Console.WriteLine(s1.LastIndexOf(searchString, position, position, StringComparison.CurrentCulture));
         Console.WriteLine(s1.LastIndexOf(searchString, position, position, StringComparison.Ordinal));
      }      

      position = s2.LastIndexOf('m');
      if (position >= 1) { 
         Console.WriteLine(s2.LastIndexOf(searchString, position, position, StringComparison.CurrentCulture));
         Console.WriteLine(s2.LastIndexOf(searchString, position, position, StringComparison.Ordinal));
      }
   }
}
// The example displays the following output:
//       4
//       3
//       3
//       -1

The following exampledemonstrates three overloads of the Overload:System.String.LastIndexOf method that find the last occurrence of a string within another string using different values of the T:System.StringComparison enumeration.

// This code example demonstrates the 
// System.String.LastIndexOf(String, ..., StringComparison) methods.

using System;
using System.Threading;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
    string intro = "Find the last occurrence of a character using different " + 
                   "values of StringComparison.";
    string resultFmt = "Comparison: {0,-28} Location: {1,3}";

// Define a string to search for.
// U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
    string CapitalAWithRing = "\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 cat = "A Cheshire c" + "\u0061\u030a" + "t";
    int loc = 0;
    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

// Clear the screen and display an introduction.
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because culture affects the result. For example, 
// try this code example with the "sv-SE" (Swedish-Sweden) culture.

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    Console.WriteLine("The current culture is \"{0}\" - {1}.", 
                       Thread.CurrentThread.CurrentCulture.Name,
                       Thread.CurrentThread.CurrentCulture.DisplayName);

// Display the string to search for and the string to search.
    Console.WriteLine("Search for the string \"{0}\" in the string \"{1}\"", 
                       CapitalAWithRing, cat);
    Console.WriteLine();

// Note that in each of the following searches, we look for 
// LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains 
// LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates 
// the string was not found.
// Search using different values of StringComparsion. Specify the start 
// index and count. 

    Console.WriteLine("Part 1: Start index and count are specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.LastIndexOf(CapitalAWithRing, cat.Length-1, cat.Length, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }

// Search using different values of StringComparsion. Specify the 
// start index. 
    Console.WriteLine("\nPart 2: Start index is specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.LastIndexOf(CapitalAWithRing, cat.Length-1, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }

// Search using different values of StringComparsion. 
    Console.WriteLine("\nPart 3: Neither start index nor count is specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.LastIndexOf(CapitalAWithRing, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }
    }
}

/*
Note: This code example was executed on a console whose user interface 
culture is "en-US" (English-United States).

This code example produces the following results:

Find the last occurrence of a character using different values of StringComparison.
The current culture is "en-US" - English (United States).
Search for the string "Å" in the string "A Cheshire ca°t"

Part 1: Start index and count are specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

Part 2: Start index is specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

Part 3: Neither start index nor count is specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

*/

ユニバーサル Windows プラットフォーム
8 以降で使用可能
.NET Framework
2.0 以降で使用可能
ポータブル クラス ライブラリ
サポート対象: 移植可能 .NET プラットフォーム
Silverlight
2.0 以降で使用可能
Windows Phone Silverlight
7.0 以降で使用可能
Windows Phone
8.1 以降で使用可能
トップに戻る
表示: