API-String类

String类(字符串类)

1.String类的概述:

String 类代表字符串,Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现;
字符串是常量;它们的值在创建之后不能更改;
字符串的本质是一个字符数组;
字符串是一种较为特殊的引用数据类型,直接输出字符串对象输出的是该对象中的数据.

 

2.String类的构造方法:

String(String original):把字符串数据封装成字符串对象;
String(char[] str):把字符数组的数据封装成字符串对象;
String(char[] value,int index,int count):把字符数组中的一部分数据封装成字符串对象,index是开始的索引值,count是字符的个数;

 

3. 通过构造方法和直接赋值方式创建的字符串对象区别:

构造方法创建的字符串对象是在堆内存中,直接赋值创建的字符串对象是在方法区的常量池.

 

4.String的转换功能:

char[] toCharArray():把字符串转换为字符数组;
String toLowerCase():把字符串转换为小写字符串;
String toUpperCase():把字符串转换为大写字符串.

 

5. String类的获取功能:

int length():获取字符串的长度,就是字符的个数;
char charAt(int index):获取指定索引处的字符,索引值必须存在,否则会报索引越界异常;
int indexOf(String str):获取字符串str在字符串对象中首次出现的索引,如果不是单个字符,则返回str中第一个字符的索引,如果不存在返回-1;
String substring(int start):从Start索引处截取到结束的子串;
String substring(int start, int end):从Start(包含)索引处截取到end索引(不包含)为止的子串.

    public static void main(String[] args) {
        // String 的基本使用

        String s1 = "abca.mp4";
        String s2 = "a,b,c,d,e,f";
        String s3 = "aa,bb,cc,dd,ee,ff";

        // 判断是否是 ""
        // System.out.println(s1.isEmpty());

        // 判断是否包含 contains
        // System.out.println(s1.contains("ca"));

        // 截取
        /**
         * System.out.println(s1.substring(2));//ca.mp4
         * System.out.println(s1.substring(2, 4));//ca
         **/

        // 切割 split
        // System.out.println(Arrays.toString(s2.split(",")));

        // 大写 小写
        // s1.toUpperCase();
        // s1.toLowerCase();

        // 去除左右的空格
        // s1.trim();

        // 替换 replace
        // System.out.println(s1.replace("a", "f"));//fbcf.mp4

        // 长度 length
        /**
         * if (s1.length() == 0) { System.out.println("请输入密码"); } else if (s1.length() <
         * 6 || s1.length() > 16) { System.out.println("不好意思 密码必须在6-16之间"); }
         **/

        // lastIndexOf 查找出现的位置
        // System.out.println(s1.lastIndexOf("a"));//3

        // indexOf 查找出现的位置
        /**
         * System.out.println(s1.indexOf("a"));//0
         * System.out.println(s1.indexOf("a",2));//3
         * System.out.println(s1.indexOf("x"));//-1
         **/

        // 是否是视频格式 endsWith
        // System.out.println(s1.endsWith(".mp4"));

        // 取字符串的第一个
        // System.out.println(s1.charAt(0));//'a'

        // equals 比较是否相等

        // 判断谁大
        // compareTo
        // compareToIgnoreCase 忽略大小写
        /**
         * int result=s1.compareTo(s2); if(result>0) { System.out.println("s1"); }else
         * if(result==0){ System.out.println("一样大"); }else { System.out.println("s2"); }
         **/

        // 判断该字符中存在多少个f
        String temp = "weursfasdfhasfjweorigdshg" + "dafhsafjsafjafoewruwrqruqw9" + "rhfidsfhasfdcjzvbvsafnakwef"
                + "afkhasfhafeiawruwahfasfhads" + "fasfasfasfasfwerwqarhfafafa";
        
        /**
        System.out.println(temp.split("f").length-1);
        **/
        
        /**
        //替换f为""
        String newTemp = temp.replace("f", "");
        System.out.println(temp.length()-newTemp.length());
        **/
        
        /**
        int count = 0;
        int idx = temp.indexOf("f");
        while (idx != -1) {
            count++;
            // 从刚刚那个位置上+1然后继续找
            idx = temp.indexOf("f", idx + 1);
        }
        System.out.println(count);
        **/
    }

 

posted @ 2022-06-11 14:30  贺梦诗  阅读(54)  评论(0)    收藏  举报