でJava、文字列indexOf()このメソッドは、指定された文字列内で指定された文字または文字列が最初に出現する位置を返します。
IndexOf() メソッドのバリアント
がある 四 IndexOf() メソッドのバリアントを以下に示します。
- intindexOf()
- int IndexOf(char ch, int strt)
- intindexOf(文字列str)
- int IndexOf(String str, int strt)
1. intindexOf()
この方法 戻り値 の 索引 この文字列内で 初め 指定された文字の出現、または文字が出現しない場合は -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'>));> >}> }> |
Pythonの改行
>
>出力
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. intindexOf(String 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));> >}> }> |
Java演算子の優先順位
>
>出力
Found geeks starting at position : 11>
4. int IndexOf(String 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>));> >}> }> |
10/100.00
>
>出力
Found geeks(after 14th index) starting at position : 19>
いくつかの関連アプリケーション
指定された文字 (大文字または小文字の場合もあります) が母音か子音かを調べます。
実装は以下のとおりです。
ジャワ
int の文字列
class> Vowels {> >// function to check if the passed> >// character is a vowel> >public> static> boolean> vowel(>char> c)> >{> >return> 'aeiouAEIOU'>.indexOf(c)>=>> >}> >// 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>