字符串的获取和字符串的截取

字符串的获取

length():获取字符串当中含有的字符个数,拿到字符串长度。

concat (String str):将当前字符串和参数字符串拼接成为返回值新的字符串。

charAt(int index):获取指定索引位置的单个字符。(索引从e开始。)

index0f(String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。

        //字符串长度
        int length = "asjfhjkdsfhjksdhfkhs".length();
        System.out.println("字符串长度:"+length);
        //拼接字符串
        String s1 = "Hello";
        String s2 = "World";
        String s3 = s1.concat(s2);
        System.out.println(s3);
        System.out.println("====================");
        //获取指定索引位置的单个字符
        char c = "Hsaq".charAt(1);
        System.out.println("在1号索引位置的字符是:"+c);
        System.out.println("====================");
        //查找参数符串在源字符串符串当中出现的第一次索引位置
        //如果没有返回-1值
        String a = "Hesadjahjf";
        int sad = a.indexOf("sad");
        System.out.println("第一次索引:"+sad);    

 

 

字符串的截取

substring(int index):截取从参数位置一直到字符串末尾,返回新字符串。

substring(int begin,int end):截取从begin开始,一直到end结束,中间的字符串。(包含左边,不包含右边)

        String s1 = "HelloWorld";
        String s2 = s1.substring(5);
        System.out.println("截取后:"+s2);
        System.out.println("==========");
        String substring = s1.substring(4, 7);
        System.out.println("从4开始到7结束:"+substring);    

 

 

 

 

 

 

 

 

posted @ 2022-06-30 11:41  魔光领域  阅读(136)  评论(0编辑  收藏  举报