Java字符串indexOf()

本文详细介绍了Java中String类的indexOf方法,包括四种不同用法:查找单个字符、从指定位置查找字符、查找子字符串以及从指定位置查找子字符串。通过示例代码展示了如何使用这些方法并解释了返回值的含义。对于找不到目标字符或子字符串的情况,indexOf会返回-1。
摘要由CSDN通过智能技术生成

Java字符串indexOf()

Java字符串的indexOf()方法返回给定的字符数值或子字符串的索引。如果找不到,则返回-1。索引计数器从零开始。


 

Java中有4种indexOf方法。

 方法描述
1int indexOf(int ch)返回给定char值的索引位置
2int indexOf(int ch,int fromIndex)返回给定char值和索引的索引位置
3int indexOf(String substring)返回给定子字符串的索引位置
4int indexOf(String substring, int fromIndex)返回给定子串的索引位置,并从索引返回

参数

ch:char值,即单个字符,例如“ a”

fromIndex:从此处获取char值或子字符串的索引的索引位置

substring:要在此字符串中搜索的子字符串


返回

字符串的索引


Java String indexOf()方法示例

 
  1. public class IndexOfExample{  
  2. public static void main(String args[]){  
  3. String s1="this is index of example";  
  4. //passing substring  
  5. int index1=s1.indexOf("is");//returns the index of is substring  
  6. int index2=s1.indexOf("index");//returns the index of index substring  
  7. System.out.println(index1+"  "+index2);//2 8  
  8.   
  9. //passing substring with from index  
  10. int index3=s1.indexOf("is",4);//returns the index of is substring after 4th index  
  11. System.out.println(index3);//5 i.e. the index of another is  
  12.   
  13. //passing char value  
  14. int index4=s1.indexOf('s');//returns the index of s char value  
  15. System.out.println(index4);//3  
  16. }} 

 

2 8
5
3

Java String indexOf(String substring)方法示例

此方法将子字符串作为参数,并返回该子字符串的第一个字符的索引。

 
  1. public class IndexOfExample2 {  
  2.     public static void main(String[] args) {  
  3.         String s1 = "This is indexOf method";         
  4.         // Passing Substring    
  5.         int index = s1.indexOf("method"); //Returns the index of this substring  
  6.         System.out.println("index of substring "+index);          
  7.     }  
  8.   
  9. }  

 

index of substring 16

Java字符串indexOf(String substring,int fromIndex)方法示例

此方法将子字符串和索引作为参数,并返回在给定fromIndex之后出现的第一个字符的索引。

 
  1. public class IndexOfExample3 {  
  2.     public static void main(String[] args) {      
  3.         String s1 = "This is indexOf method";         
  4.         // Passing substring and index  
  5.         int index = s1.indexOf("method", 10); //Returns the index of this substring  
  6.         System.out.println("index of substring "+index);  
  7.         index = s1.indexOf("method", 20); // It returns -1 if substring does not found  
  8.         System.out.println("index of substring "+index);          
  9.     }  
  10. }  

 

index of substring 16
index of substring -1

Java字符串indexOf(int char,int fromIndex)方法示例

此方法将char和index作为参数,并返回在给定fromIndex之后出现的第一个字符的索引。

 
  1. public class IndexOfExample4 {  
  2.     public static void main(String[] args) {          
  3.         String s1 = "This is indexOf method";         
  4.         // Passing char and index from  
  5.         int index = s1.indexOf('e', 12); //Returns the index of this char  
  6.         System.out.println("index of char "+index);       
  7.     }  
  8.  

 

index of char 17
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值