当前位置:   article > 正文

字符串查找——java的charAt方法和indexOf方法详解_java字符串数组元素查找子串

java字符串数组元素查找子串

1.按下标查找
char charAt(int index)
返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常

public class Test {
    public static void main(String[] args) {
        String name = "baixian";
        for (int i = 0; i < name.length(); i++) {
            System.out.println(name.charAt(i));//下标从0开始
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

结果是
b
a
i
x
i
a
n
String.java中对该方法定义如下

 public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);//越界,扔出警告
        }
        return value[index];//如果没错,就返回字符数组对应下标的元素
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.按字符查找

在这里插入图片描述

public class Test {
    public static void main(String[] args) {
        String name = "baixian";
        System.out.println(name.indexOf('a'));//打印值为1
        System.out.println(name.indexOf('a',2));//从下标2的位置开始遍历,打印值为5
        System.out.println(name.indexOf("ai"));//在"baixian"主串中寻找子串(默认从0下标开始),返回子串的首字母所在的下标,打印值为1
        System.out.println(name.indexOf("ai",3));//在"baixian"主串中从3下标开始寻找子串,返回子串的首字母所在的下标,打印值为-1
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

String.java中对该方法定义如下

 public int indexOf(int ch) {
        return indexOf(ch, 0);
    }
  • 1
  • 2
  • 3
public int indexOf(int ch, int fromIndex) {//fromInde表示起始下标
        final int max = value.length;
        if (fromIndex < 0) {//起始下标小于0默认从从0开始
            fromIndex = 0;
        } else if (fromIndex >= max) {//起始下标大于字符串长度,返回-1
            // Note: fromIndex might be near -1>>>1.
            return -1;
        }

        if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
            // handle most cases here (ch is a BMP code point or a
            // negative value (invalid code point))
            final char[] value = this.value;
            for (int i = fromIndex; i < max; i++) {//遍历寻找
                if (value[i] == ch) {
                    return i;
                }
            }
            return -1;
        } else {
            return indexOfSupplementary(ch, fromIndex);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
public int indexOf(String str, int fromIndex) {
        return indexOf(value, 0, value.length,
                str.value, 0, str.value.length, fromIndex);
    }
  • 1
  • 2
  • 3
  • 4

当然还有,这里就不一一列举出来了。

3.int lastIndexOf()

int lastIndexOf(int ch) 从后往前找,返回ch第一次出现的位置,没有返回-1
int lastIndexOf(int ch, int fromIndex)从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1
int lastIndexOf(String str) 从后往前找,返回str第一次出现的位置,没有返回-1
int lastIndexOf(String str, intfromIndex)从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返
回-1

public class Test {
    public static void main(String[] args) {
        String name = "baixian";
        System.out.println(name.lastIndexOf('a'));
        System.out.println(name.lastIndexOf('a',6));
        System.out.println(name.lastIndexOf("ai"));
        System.out.println(name.lastIndexOf("ai",4));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/947030
推荐阅读
相关标签
  

闽ICP备14008679号