字符串获取

发布时间 2023-04-18 19:54:04作者: harper886

字符串获取

字符串获取函数

  1. 获取字符串长度
  2. 连接两个字符串并返回新的字符串(!!!重点!!!在Java中字符串是不可改变的)
  3. 获取索引位置的单个字符
  4. 字串查找返回第一次出现的索引,没有返回-1

image-20230418193620462

代码示例

public class Demo02Str {
    public static void main(String[] args) {
        System.out.println("jdoljdofjaofeyrowefjljcdxc".length());//获取字符串长度
        String str1 = "hello";
        String str2 = "world";
        String str3 = str1.concat(str2);
        //在java中字符串不可改变
        System.out.println(str1);//hello
        System.out.println(str2);//world
        System.out.println(str3);
        String str4 = "abcdefg";
        for (int i = 0; i < str4.length(); i++) {
            char ch = str4.charAt(i);

            System.out.println("第" + i + "个位置是字母" + ch);
        }
        System.out.println("abcdefabcdef".indexOf("bcde"));//1
        System.out.println("abcdef".indexOf("aaaa"));//-1

    }
}