《String类常见的API》: 字符串的查询操作。

目录

前言:

一、indexOf() 方法

1、int indexOf(int ch)

2、int indexOf(int ch, int fromIndex)

3、int indexOf(String str)

4、int indexOf(String str, int fromIndex)

二、lastIndexOf() 方法 

1、int lastIndexOf(int ch)

2、int lastIndexOf(int ch, int fromIndex)

3、int lastIndexOf(String str)

 三、startsWith() 方法

四、endsWith() 方法


前言:

                                   依托秉性和才智做事

        秉性和才智,共同决定着你的天赋是否能得到充分的发挥,两者缺一不可,如果丢掉了其中的一个,你就只能获取一半的成功。只拥有才智是不够的,你还需要拥有适合它的秉性。那些蠢人之所以不幸,是因为没有合适他们的地位、出生以及朋友圈子。

一、indexOf() 方法

1、int indexOf(int ch)

方法声明:

        int indexOf(int ch)

功能描述:

        返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

代码示例:

String str1 = "study hard,study";

System.out.println("第一个y所在的索引位置是"+str1.indexOf("y"));

运行结果:

        第一个y所在的索引位置是4。

注意:

        位置索引是从0开始,并且空格或字符也算一个位置。

2、int indexOf(int ch, int fromIndex)

方法声明:

        int indexOf(int ch, int fromIndex)

功能描述:

        返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

代码示例:

String str1 = "study hard,study";

		
System.out.println("从6位置开始查找指定字符在字符串中第一次出现处的索是"+str1.indexOf("y",6)); 

运行结果:

        从6位置开始查找指定字符在字符串中第一次出现处的索引是15。

3、int indexOf(String str)

方法声明:

       int indexOf(String str)

功能描述:

         返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

代码示例:

String str1 = "study hard,study";

System.out.println("第一个hard所在的索引位置是"+str1.indexOf("hard"));

运行结果:

        第一个hard所在的索引位置是6。

4、int indexOf(String str, int fromIndex)

方法声明:

       int indexOf(String str, int fromIndex)

功能描述:

         返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

代码示例:

String str1 = "study hard,study";

System.out.println("从6位置开始查找指定字符在字符串中第一次出现study的索引是"+str1.indexOf("study",6));

运行结果:

        从6位置开始查找指定字符在字符串中第一次出现hard的索引是11。

二、lastIndexOf() 方法 

1、int lastIndexOf(int ch)

方法声明:

        int lastindexOf(int ch)

功能描述:

        返回指定字符在字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

代码示例:

String str1 = "study hard,study";

System.out.println("最后一个y所在的索引位置是"+str1.lastIndexOf("y"));

运行结果:

        最后一个y所在的索引位置是15。

2、int lastIndexOf(int ch, int fromIndex)

方法声明:

        int lastIndexOf(int ch, int fromIndex)

功能描述:

        返回从 fromIndex 位置开始反向查找指定字符在字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

代码示例:

String str1 = "study hard,study";

		
System.out.println("从8位置开始反向查找指定字符在字符串中最后一次出现处的索是"+str1.lastIndexOf("y",8)); 

运行结果:

        从8位置开始反向查找指定字符在字符串中最后一次出现处的索是4。

3、int lastIndexOf(String str)

方法声明:

       int lastIndexOf(String str)

功能描述:

         返回指定字符在字符串中最右边的出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

代码示例:

String str1 = "study hard,study";

System.out.println("最右边的study所在的索引位置是"+str1.lastIndexOf("study"));

运行结果:

        最右边的study所在的索引位置是11。

 三、startsWith() 方法

方法声明:

        boolean startsWith(String prefix)

功能描述:

        判断此字符串是否以指定的前缀开始,是,返回true;不是,返回false。

代码示例:

String str1 = "study hard,study";

System.out.println("此字符串是否以study开头:"+str1.startsWith("study"));

System.out.println("此字符串是否以stady开头:"+str1.startsWith("stady"));

运行结果:

        此字符串是否以study开头:true
        此字符串是否以stady开头:false

四、endsWith() 方法

方法声明:

        boolean endsWith(String prefix)

功能描述:

        判断此字符串是否以指定的后缀结束,是,返回true;不是,返回false。

代码示例:

String str1 = "study hard,study";

System.out.println("此字符串是否以study结束:"+str1.startsWith("study"));

System.out.println("此字符串是否以stady结束:"+str1.startsWith("stady"));

运行结果:

        此字符串是否以study结束:true
        此字符串是否以stady结束:false

猜你喜欢

转载自blog.csdn.net/yzh2776680982/article/details/124537575