Java字符串indexOf()

indexOf()方法的变体。本文对所有这些问题进行了如下描述: 1.int indexOf() :这个方法 返回 这个 指数 在这串 第一 指定字符的出现,如果该字符未出现,则为-1。

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

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


输出

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

// 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

// 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

// 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

一些相关应用:

  • 找出给定的字符(可能是大写或小写)是元音还是辅音。 具体实施情况如下:

JAVA

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" );
}
}
// This code is contributed by debjitdbb


输出

Vowel

本文由 阿斯塔·蒂亚吉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享