C#/문자열

[C#]문자열 뒤에서 찾기(LastIndexOf)

DevStory 2021. 8. 22.

문자열을 뒤에서 찾아야 하는 경우 LastIndexOf() 메서드를 사용하여 찾을 수 있습니다.

 

이번 포스팅에서는 LastIndexOf() 메서드를 사용하여 문자열을 역순으로 검색하여 마지막 위치와 가까운 문자 또는 문자열의 위치를 찾는 방법을 정리합니다.

 


LastIndexOf 메서드

문자열의 마지막 위치에서부터 검색하고자 하는 문자 또는 문자열의 인덱스를 찾습니다.

 

동일한 문자열이 존재할 경우 마지막 위치와 가까운 문자열의 위치(인덱스)를 반환합니다.

 

찾는 문자 또는 문자열이 없을 경우 -1을 반환합니다.

 

"Hello Hello Hello" 라는 문자열에서 "Hello"의 위치를 검색하는 과정입니다. 

 

역순으로 검색하여 마지막 위치와 가까운 "Hello"의 위치를 반환합니다.


LastIndexOf 메서드 사용방법

LastIndexOf 메서드는 여러 가지 오버로딩이 존재합니다.

 

오버로딩된 LastIndexOf 메서드 사용 방법을 정리하였습니다.

 

☞ StringComparison 열거형이란?

 

LastIndexOf(String str, Int32 startIndex, Int32 length)

지정된 위치에서 역순으로 지정된 문자의 수만큼 특정 문자열을 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

// str의 마지막 위치에서 str.Length개의 문자열을 대상으로 "LastIndexOf"의 위치를 찾습니다.
Console.WriteLine(str.LastIndexOf("LastIndexOf", str.Length - 1, str.Length));

실행 결과

 

LastIndexOf(String str, Int32 startIndex, Int32 length, StringComparison)

지정된 위치에서 역순으로 지정된 문자의 수만큼 특정 문자열을 검색합니다.

검색 유형을 설정할 수 있습니다.

string str = "IndexOf Method LastIndexOf Method";

// 대소문자를 무시하는 검색 유형을 설정합니다.
Console.WriteLine(str.LastIndexOf("lastindexof", str.Length - 1, str.Length, StringComparison.OrdinalIgnoreCase));

실행 결과

 

LastIndexOf(String str, Int32 startIndex, StringComparison)

지정된 위치에서 문자열의 시작 위치(0 번째 인덱스)까지 특정 문자열을 검색합니다.

검색 유형을 설정할 수 있습니다.

string str = "IndexOf Method LastIndexOf Method";

// 대소문자를 무시하는 검색 유형을 설정합니다.
Console.WriteLine(str.LastIndexOf("Method Last", str.Length - 1, StringComparison.OrdinalIgnoreCase));

실행 결과

 

LastIndexOf(Char char, Int32 startIndex, Int32 length)

지정된 위치에서 역순으로 지정된 문자의 수만큼 문자를 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

// str의 마지막 위치에서 str.Length개의 문자열을 대상으로 'L'의 위치를 찾습니다.
Console.WriteLine(str.LastIndexOf('L', str.Length - 1, str.Length));

실행 결과

 

LastIndexOf(String str, StringComparison)

문자열의 마지막 위치에서 문자열의 시작 위치(0 번째 인덱스)까지 특정 문자열을 검색합니다.

검색 규칙을 설정할 수 있습니다.

string str = "IndexOf Method LastIndexOf Method";

// 대소문자를 무시하는 검색 유형을 설정합니다.
Console.WriteLine(str.LastIndexOf("indexof", StringComparison.OrdinalIgnoreCase));

실행 결과

 

LastIndexOf(String str, Int32 startIndex)

지정된 위치에서 문자열의 시작 위치(0 번째 인덱스)까지 특정 문자열을 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

// str.Length = 33
// 13번째 인덱스부터 역순으로 "IndexOf"의 위치를 찾습니다.
Console.WriteLine(str.LastIndexOf("IndexOf", str.Length - 20));

실행 결과

 

LastIndexOf(Char char, Int32 startIndex)

지정된 위치에서 문자열의 시작 위치(0 번째 인덱스)까지 특정 문자를 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

// 0번째 위치에서 역순으로 'e'를 검색합니다.
Console.WriteLine(str.LastIndexOf('e', str.Length - 1));

// 8번째 위치에서 역순으로 'e'를 검색합니다.
Console.WriteLine(str.LastIndexOf('e', 8));

실행 결과

 

LastIndexOf(String str)

문자열의 마지막 위치에서 문자열의 시작 위치(0 번째 인덱스)까지 특정 문자열을 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

Console.WriteLine(str.LastIndexOf("Last"));

실행 결과

 

LastIndexOf(Char char)

문자열의 마지막 위치에서 문자열의 시작 위치(0 번째 인덱스)까지 특정 문자를 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

Console.WriteLine(str.LastIndexOf('L'));

실행 결과


LastIndexOf 메서드 주의사항

시작 위치가 문자열의 길이를 초과하거나 0 미만이면 예외가 발생합니다. 

string str = "IndexOf Method LastIndexOf Method";

Console.WriteLine(str.LastIndexOf("Method", str.Length + 1, str.Length));

실행 결과

 

(문자의 수 - 시작 위치)는 1을 초과할 수 없습니다.

string str = "IndexOf Method LastIndexOf Method";

Console.WriteLine(str.LastIndexOf("Method", str.Length, str.Length + 1));

Console.WriteLine(str.LastIndexOf("Method", str.Length, str.Length + 2));

실행 결과

str.LastIndexOf("Method", str.Length, str.Length + 1)는 문자의 수 - 시작 위치가 1이므로 예외가 발생하지 않습니다.

 

하지만, str.LastIndexOf("Method", str.Length, str.Length + 2)는 문자의 수 - 시작 위치가 2이므로 예외가 발생합니다. 

 

문자열 마지막 위치에서부터 검색할려면 지정된 위치를 str.Length - 1로 설정해야합니다.

string str = "IndexOf Method LastIndexOf Method";

Console.WriteLine("str.Length : " + str.Length);

Console.WriteLine(str.LastIndexOf("d", str.Length - 1, 1));

Console.WriteLine(str.LastIndexOf("d", str.Length, 1));

실행 결과

 

반응형

댓글