zoukankan      html  css  js  c++  java
  • Java中indexOf的用法

    indexOf有四种用法:

    1.indexOf(int ch) 在给定字符串中查找字符(ASCII),找到返回字符数组所对应的下标找不到返回-1

    2.indexOf(String str)在给定符串中查找另一个字符串。。。

    3.indexOf(int ch,int fromIndex)从指定的下标开始查找某个字符,查找到返回下标,查找不到返回-1

    4.indexOf(String str,int fromIndex)从指定的下标开始查找某个字符串。。。

    public class Test {
    
    	public static void main(String[] args) {
    		char[] ch= {'a','b','c','h','e','l','l','o'};
    		String str=new String(ch);
    
    		//1.indexOf(int ch)
    		str.indexOf(104);
    		System.out.println(str.indexOf(104));//h所在下标为3
    
    		//2.indexOf(String str)
    		str.indexOf("hell");
    		System.out.println(str.indexOf("hell"));//3
    
    		//3.indexOf(int ch,int fromIndex)
    		str.indexOf(101, 4);//4
    		System.out.println(str.indexOf(101, 4));
    		str.indexOf(101,5);//没有查找到返回-1
    		System.out.println(str.indexOf(101,5));
    
    		//4.indexOf(String str,int fromIndex)
    		str.indexOf("che", 0);//等价于str.indexOf("che")
    		System.out.println(str.indexOf("che", 0));//2
    	}
    }
    

      

  • 相关阅读:
    我们是在开发产品还是项目?
    创业期的软件开发管理(一)
    由“I”到“T”
    创业期的软件开发管理(二)
    平台架构用户系统
    产品的臃肿过程
    平台架构——体系结构
    狼群的架构暗示
    如何创建一个好的索引
    哈希索引
  • 原文地址:https://www.cnblogs.com/xiaoke111/p/7660707.html
Copyright © 2011-2022 走看看