ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

|NO.Z.00025|——————————|BigDataEnd|——|Java&核心类库.V10|----------------------------------------------|Ja

2022-04-04 15:04:10  阅读:177  来源: 互联网

标签:Java v10 pos System 查找 println Day str1 out




[BigDataJava:Java&核心类库.V10]                                                                             [BigDataJava.核心类库][|章节二|string类型实现字符和字符串的正向查找|反向查找|]








一、string类型实现字符和字符串的正向查找:方法声明
方法声明功能介绍
int indexOf(int ch)用于返回当前字符串中参数ch指定的字符第一次出现的下标
int indexOf(int ch, int fromIndex)用于从fromIndex位置开始查找ch指定的字符
int indexOf(String str)在字符串中检索str返回其第一次出现的位置,若找不到返回-1
int indexOf(String str, intfromIndex)表示从字符串的fromIndex位置开始检索str第一次出现的位置
int lastIndexOf(int ch)用于返回参数ch指定的字符最后一次出现的下标
int lastIndexOf(int ch, intfromIndex)用于从fromIndex位置开始查找ch指定字符出现的下标
int lastIndexOf(String str)返回str指定字符串最后一次出现的下标
int lastIndexOf(String str,intfromIndex用于从fromIndex位置开始反向搜索的第一次出现的下标。

### --- 案例题目

~~~     ——>        编写通用的代码可以查询字符串"Good Good Study, Day Day Up!
~~~     ——>       "中所有"Day"出现的索引位置并打印出来。
二、编程代码
package com.yanqi.task12;

public class StringIndexTest {

    public static void main(String[] args) {

        // 1.构造String类型的对象并打印
        String str1 = new String("Good Good Study, Day Day Up!");
        System.out.println("str1 = " + str1); // Good Good Study, Day Day Up!

        // 2.实现字符串中指定字符和字符串的查找功能
        int pos = str1.indexOf('g');
        System.out.println("pos = " + pos); // -1  代表查找失败
        pos = str1.indexOf('G');
        System.out.println("pos = " + pos); // 0   该字符第一次出现的索引位置
        // 表示从下标0开始查找字符'G'第一次出现的索引位置,包含0
        pos = str1.indexOf('G', 0);
        System.out.println("pos = " + pos); // 0
        pos = str1.indexOf('G', 1);
        System.out.println("pos = " + pos); // 5

        System.out.println("------------------------------------------------------");
        // 查找字符串
        pos = str1.indexOf("day");
        System.out.println("pos = " + pos); // -1
        pos = str1.indexOf("Day");
        System.out.println("pos = " + pos); // 17   字符串中第一个字符的下标
        pos = str1.indexOf("Day", 17);
        System.out.println("pos = " + pos); // 17   字符串中第一个字符的下标
        pos = str1.indexOf("Day", 18);
        System.out.println("pos = " + pos); // 21   字符串中第一个字符的下标

        System.out.println("------------------------------------------------------");
        // 编写通用代码实现将字符串str1中所有"Day"出现的索引位置找到并打印出来
        pos = str1.indexOf("Day");
        while (-1 != pos) {
            System.out.println("pos = " + pos); // 17
            pos = str1.indexOf("Day", pos+1);
        }

        System.out.println("------------------------------------------------------");
        // 优化一下
        pos = 0;
        while ((pos = str1.indexOf("Day", pos)) != -1) {
            System.out.println("pos = " + pos);
            pos += "Day".length();
        }

        System.out.println("------------------------------------------------------");
        // 3.实现字符和字符串内容的反向查找
        pos = str1.lastIndexOf('G');
        System.out.println("pos = " + pos); // 5
        // 从下标5的位置开始反向查找
        pos = str1.lastIndexOf('G', 5);
        System.out.println("pos = " + pos); // 5

        pos = str1.lastIndexOf('G', 6);
        System.out.println("pos = " + pos); // 5

        pos = str1.lastIndexOf('G', 4);
        System.out.println("pos = " + pos); // 0

        System.out.println("------------------------------------------------------");
        pos = str1.lastIndexOf("Day");
        System.out.println("pos = " + pos); // 21
        pos = str1.lastIndexOf("Day",  21);
        System.out.println("pos = " + pos); // 21
        pos = str1.lastIndexOf("Day", 20);
        System.out.println("pos = " + pos); // 17
        pos = str1.lastIndexOf("Day", 15);
        System.out.println("pos = " + pos); // -1
    }
}
三、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=52251:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task12.StringIndexTest
str1 = Good Good Study, Day Day Up!
pos = -1
pos = 0
pos = 0
pos = 5
------------------------------------------------------
pos = -1
pos = 17
pos = 17
pos = 21
------------------------------------------------------
pos = 17
pos = 21
------------------------------------------------------
pos = 17
pos = 21
------------------------------------------------------
pos = 5
pos = 5
pos = 5
pos = 0
------------------------------------------------------
pos = 21
pos = 21
pos = 17
pos = -1

Process finished with exit code 0








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                   ——W.S.Landor



来自为知笔记(Wiz)

标签:Java,v10,pos,System,查找,println,Day,str1,out
来源: https://www.cnblogs.com/yanqivip/p/16099156.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有