C#/문자열

[C#]문자열 위치 찾기(IndexOf)

DevStory 2021. 7. 29.

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

 

이번 포스팅에서는 IndexOf() 메서드를 사용하여 시작 위치와 가까운 문자 또는 문자열의 위치를 찾는 방법을 정리합니다.

 


IndexOf 메서드

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

 

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

 

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


IndexOf 메서드 사용 방법

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

 

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

 

☞ StringComparison 열거형이란?

 

IndexOf(String str, Int32 startIndex, Int32 length)

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

string str = "IndexOf Method LastIndexOf Method";

// 0번째 위치에서 str.Lengthr개의 문자열을 대상으로 "Method"를 찾습니다.
Console.WriteLine(str.IndexOf("Method", 0, str.Length));

// 0번째 위치에서 4개의 문자열을 대상으로 "Method"를 찾습니다.
Console.WriteLine(str.IndexOf("Method", 0, 4));

실행 결과

 

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

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

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

string str = "IndexOf Method LastIndexOf Method";

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

// 검색 유형이 없을경우 대소문자를 구분합니다. 
Console.WriteLine(str.IndexOf("method", 0, str.Length));

실행 결과

 

IndexOf(String str, Int32 startIndex, StringComparison)

지정된 위치에서 문자열의 마지막까지 특정 문자열을 검색합니다.

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

string str = "IndexOf Method LastIndexOf Method";

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

실행 결과

 

IndexOf(Char char, Int32 startIndex, Int32 length)

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

string str = "IndexOf Method LastIndexOf Method";

// 0번째 위치에서 str.Lengthr개의 문자열을 대상으로 'e'의 위치를 찾습니다.
// 동일한 문자가 여러개 존재할경우 맨 앞에 index를 반환합니다.
Console.WriteLine(str.IndexOf('e', 0, str.Length));

// 8번째 위치(M)부터 6개의 문자열을 대상으로 'e'의 위치를 찾습니다.
Console.WriteLine(str.IndexOf('e', 8, 6));

실행 결과

 

IndexOf(String str, StringComparison)

첫 번째 위치에서 문자열의 마지막까지 특정 문자열을 검색합니다.

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

string str = "IndexOf Method LastIndexOf Method";

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

실행 결과

 

IndexOf(Char char, StringComparison)

첫 번째 위치에서 문자열의 마지막까지 특정 문자를 검색합니다.

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

string str = "IndexOf Method LastIndexOf Method";

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

실행 결과

 

IndexOf(String str, Int32 startIndex)

지정된 위치에서 문자열의 마지막까지 특정 문자열을 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

// 0번째 위치에서 "Of"를 검색합니다.
Console.WriteLine(str.IndexOf("Of", 0));

// 8번째 위치에서 "Of"를 검색합니다.
Console.WriteLine(str.IndexOf("Of", 8));

실행 결과

 

IndexOf(Char char, Int32 startIndex)

지정된 위치에서 문자열의 마지막까지 특정 문자를 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

// 0번째 위치에서 'e'를 검색합니다.
Console.WriteLine(str.IndexOf('e', 0));

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

실행 결과

 

IndexOf(String str)

첫 번째 위치에서 문자열의 마지막까지 특정 문자열을 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

Console.WriteLine(str.IndexOf("Of"));

실행 결과

 

IndexOf(Char char)

첫 번째 위치에서 문자열의 마지막까지 특정 문자를 검색합니다.

string str = "IndexOf Method LastIndexOf Method";

Console.WriteLine(str.IndexOf('e'));

실행 결과


IndexOf 메서드 특징 및 주의사항

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

string str = "IndexOf Method LastIndexOf Method";

Console.WriteLine(str.IndexOf("Of", 100));

실행 결과

문자열 str의 길이는 33입니다.

33를 초과한 값을 시작 위치로 설정하면 예외가 발생합니다.

 

시작 위치 + 문자의 수는 문자열의 길이를 초과할 수 없습니다.

string str = "IndexOf Method LastIndexOf Method";

// 8번째 위치에서 6개의 문자열은 "Method"
Console.WriteLine(str.IndexOf("e", 8, 6));

// 16번째 위치에서 18개의 문자열은 "LastIndexOf Method "
Console.WriteLine(str.IndexOf("e", 16, 18));

실행 결과

시작 위치(16) + 지정된 길이(18)은 문자열의 길이(33)를 초과하여 예외가 발생합니다.

 

반응형

댓글