자바 문자열 indexOf()

~ 안에 자바, 문자열 indexOf() 메소드는 지정된 문자열에서 지정된 문자 또는 문자열이 처음 나타나는 위치를 반환합니다.

indexOf() 메서드의 변형

있다 indexOf() 메소드의 변형은 아래에 언급되어 있습니다:

  • 정수 indexOf()
  • int indexOf(char ch, int strt)
  • int indexOf(문자열 str)
  • int indexOf(문자열 str, int strt)

1. 정수 indexOf()

이 방법 보고 그만큼 색인 이 문자열 내에서 첫 번째 지정된 문자가 발생하거나 해당 문자가 발생하지 않으면 -1입니다.

 Syntax: int indexOf(char ch ) Parameters: ch : a character. 

아래는 위의 방법을 구현한 것입니다.

자바




// Java code to demonstrate the working> // of String indexOf()> public> class> Index1 {> > public> static> void> main(String args[])> > {> > // Initialising String> > String gfg => new> String(> 'Welcome to geeksforgeeks'> );> > System.out.print(> 'Found g first at position : '> );> > // Initial index of 'g' will print> > // prints 11> > System.out.println(gfg.indexOf(> 'g'> ));> > }> }>

산출

Found g first at position : 11 

2. int indexOf(char ch, int strt)

이 방법 보고 이 문자열 내의 인덱스 첫 번째 지정된 문자가 발생하면 지정된 인덱스에서 검색을 시작하고 문자가 발생하지 않으면 -1입니다.

 Syntax: int indexOf(char ch, int strt) Parameters: ch  :a character. strt : the index to start the search from. 

위 방법의 예:

자바




// Java code to demonstrate the working> // of String indexOf(char ch, int strt)> public> class> Index2 {> > public> static> void> main(String args[])> > {> > // Initialising String> > String gfg => new> String(> 'Welcome to geeksforgeeks'> );> > System.out.print(> > 'Found g after 13th index at position : '> );> > // 2nd index of 'g' will print> > // prints 19> > System.out.println(gfg.indexOf(> 'g'> ,> 13> ));> > }> }>

산출

Found g after 13th index at position : 19 

3. int indexOf(문자열 str)

이 방법 보고 이 문자열 내의 인덱스 첫 번째 지정된 항목의 발생 하위 문자열 . 하위 문자열로 발생하지 않으면 -1이 반환됩니다.

 Syntax: int indexOf(String str) Parameters: str : a string. 

위 방법의 예:

자바




// Java code to demonstrate the working> // of String indexOf(String str)> public> class> Index3 {> > public> static> void> main(String args[])> > {> > // Initialising string> > String Str => new> String(> 'Welcome to geeksforgeeks'> );> > // Initialising search string> > String subst => new> String(> 'geeks'> );> > // print the index of initial character> > // of Substring> > // prints 11> > System.out.print(> > 'Found geeks starting at position : '> );> > System.out.print(Str.indexOf(subst));> > }> }>

산출

Found geeks starting at position : 11 

4. int indexOf(문자열 str, int strt)

이 방법 보고 이 문자열 내의 인덱스 첫 번째 지정된 항목의 발생 하위 문자열 , 시작 지정된 시간에 색인 . 발생하지 않으면 -1이 반환됩니다.

 Syntax: int indexOf(String str, int strt) Parameters: strt : the index to start the search from. str : a string. 

자바




// Java code to demonstrate the working> // of String indexOf(String str, int strt)> public> class> Index4 {> > public> static> void> main(String args[])> > {> > // Initialising string> > String Str => new> String(> 'Welcome to geeksforgeeks'> );> > // Initialising search string> > String subst => new> String(> 'geeks'> );> > // print the index of initial character> > // of Substring after 14th position> > // prints 19> > System.out.print(> > 'Found geeks(after 14th index) starting at position : '> );> > System.out.print(Str.indexOf(subst,> 14> ));> > }> }>

산출

Found geeks(after 14th index) starting at position : 19 

일부 관련 응용 프로그램

주어진 문자(대문자나 소문자 등)가 모음인지 자음인지 알아냅니다.

구현은 다음과 같습니다.

자바




class> Vowels {> > // function to check if the passed> > // character is a vowel> > public> static> boolean> vowel(> char> c)> > {> > return> 'aeiouAEIOU'> .indexOf(c)>=> 0> ;> > }> > // Driver program> > public> static> void> main(String[] args)> > {> > boolean> isVowel = vowel(> 'a'> );> > // Printing the output> > if> (isVowel)> > System.out.println(> 'Vowel'> );> > else> > System.out.println(> 'Consonant'> );> > }> }>

산출

Vowel