logo

Java String indexOf()

noterJava, niz indexOf()metoda vrne položaj prve pojavitve podanega znaka ali niza v podanem nizu.

Različice metode indexOf().

obstajajo štiri različice metode indexOf() so omenjene spodaj:

  • int indexOf()
  • int indexOf(char ch, int strt)
  • int indexOf(String str)
  • int indexOf(String str, int strt)

1. int indexOf()

Ta metoda vrača the kazalo znotraj tega niza prvi pojavitev podanega znaka ali -1, če se znak ne pojavi.



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

Spodaj je izvedba zgornje metode

Java




// 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'>));> >}> }>

pretvori strin v int
>

>

Izhod

Found g first at position : 11>

2. int indexOf(char ch, int strt)

Ta metoda vrača indeks znotraj tega niza prvi pojavitev podanega znaka, začetek iskanja pri podanem indeksu ali -1, če se znak ne pojavi.

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

Primer zgornje metode:

Java




// 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>));> >}> }>

>

java razvrščanje seznama

>

Izhod

Found g after 13th index at position : 19>

3. int indexOf(String str)

Ta metoda vrača indeks znotraj tega niza prvi pojav navedenega podniz . Če se ne pojavi kot podniz, se vrne -1.

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

Primer zgornje metode:

Java




// 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));> >}> }>

kaj je android velikonočno jajce
>

>

disketa
Izhod

Found geeks starting at position : 11>

4. int indexOf(String str, int strt)

Ta metoda vrača indeks znotraj tega niza prvi pojav navedenega podniz , zagon ob določenem kazalo . Če se ne pojavi, se vrne -1.

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

Java




// 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>));> >}> }>

>

>

Izhod

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

Nekatere sorodne aplikacije

Ugotavljanje, ali je določen znak (morda kar koli z velikimi ali malimi črkami) samoglasnik ali soglasnik.

Izvedba je navedena spodaj:

Java


gimp, kako preklicati izbiro



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'>);> >}> }>

>

>

Izhod

Vowel>