이 문서는 기계 번역을 이용하여 번역되었습니다. 문서를 영문으로 보려면 영문 확인란을 선택하세요. 마우스 포인터를 텍스트 위로 이동시켜 팝업 창에서 영문 텍스트를 표시할 수도 있습니다.
번역
영문

String.LastIndexOf 메서드 (String, Int32, StringComparison)

 

현재 String 개체에서 마지막으로 발견되는 지정된 문자열의 0부터 시작하는 인덱스를 보고합니다. 지정된 문자 위치에서 시작하고 문자열의 시작 부분을 향해 뒤로 검색이 진행됩니다. 매개 변수는 지정된 문자열을 검색할 때 수행할 비교 유형을 지정합니다.

네임스페이스:   System
어셈블리:  mscorlib(mscorlib.dll에 있음)

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

매개 변수

value
Type: System.String

검색할 문자열입니다.

startIndex
Type: System.Int32

검색을 시작할 위치입니다. startIndex에서 이 인스턴스의 시작 부분을 향해 검색이 진행됩니다.

comparisonType
Type: System.StringComparison

검색 규칙을 지정하는 열거형 값 중 하나입니다.

반환 값

Type: System.Int32

해당 문자열이 있으면 value 매개 변수의 인덱스 위치(0부터 시작)이고, 해당 문자열이 없거나 현재 인스턴스가 String.Empty와 같으면 -1입니다. valueString.Empty이면 반환 값은 startIndex와 이 인스턴스의 마지막 인덱스 위치 중 작은 값입니다.

Exception Condition
ArgumentNullException

valuenull인 경우

ArgumentOutOfRangeException

현재 인스턴스가 String.Empty와 같지 않고 startIndex가 0보다 작거나 현재 인스턴스의 길이보다 큽니다.

또는

현재 인스턴스가 String.Empty와 같고 startIndex가 -1보다 작거나 0보다 큽니다.

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 the first character position has been examined. For example, if startIndex is P:System.String.Length - 1, the method searches every character from the last character in the string to the beginning.

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.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.StringComparison) method is used to find the position of a soft hyphen (U+00AD) followed by an "m", starting with 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. 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". 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 >= 0) {
         Console.WriteLine(s1.LastIndexOf(searchString, position, StringComparison.CurrentCulture));
         Console.WriteLine(s1.LastIndexOf(searchString, position, StringComparison.Ordinal));
      }

      position = s2.LastIndexOf('m');
      if (position >= 0) {
         Console.WriteLine(s2.LastIndexOf(searchString, position, StringComparison.CurrentCulture));
         Console.WriteLine(s2.LastIndexOf(searchString, 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 이후 사용 가능
맨 위로 이동
표시: