复习String类型常用的api

String的常用api

public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("zhangsan");
        String s2= new String("zhangsan");
        /**
         * 问:一共创建了几个对象?
         *
         * 答:三个,方法区字符串常量池中有一个,堆内存中有两个String对象
         */
        //将byte数组转化为String
        byte[] bytes ={97,98,99};
        String s3 = new String(bytes);
        System.out.println(s3);//abc

        //将bytes数组中的一部分转化为字符串
        String s4 = new String(bytes,1,2);
        System.out.println(s4);//bc

        //char charAt(int index)
        //将字符串中的某个元素转化为字符
        char c = "好学生".charAt(0);
        System.out.println(c);//好

        //int comparator(String anotherString )
        //按照字典中字母顺序进行比较
        int i = "abc".compareTo("a");
        System.out.println(i);//2
        int i1 = "b".compareTo("a");
        System.out.println(i1);//1
        int i2 = "a".compareTo("a");
        System.out.println(i2);//0

        //boolean contains(charSequence s)
        //判断某字符串中是否包含子字符串
        boolean a = "abcdef".contains("a");
        System.out.println(a);//true

        //boolean endsWith(String s)
        //判断某字符串是否以某子字符串结尾
        boolean fg = "abcdefg".endsWith("fg");
        System.out.println(fg);//true

        //boolean isEmpty()
        //判断某字符串是否为空字符串
        String s5="";
        String s6 = "a";
        System.out.println(s5.isEmpty());//true
        System.out.println(s6.isEmpty());//false

        //int length()
        //计算字符串的长度
        System.out.println("abc".length());//3
        System.out.println("dsahdjsadjkas".length());//13

        //int lastIndexOf()
        //判断某个子字符串在当前某字符串中最后一次出现的位置
        System.out.println("javac++phpjava".lastIndexOf("java"));//10

        //String replace
        //把字符串中的某个子字符串替换掉
        String cc = "zhangsan".replace("san", "si");
        System.out.println(cc);//zhangsi

        //String[] split(String s)
        //拆分字符串
        String[] strings = "2020-2-2".split("-");
        for (String s : strings){
            System.out.println(s);//2020 2 2
        }

        //boolean startWith(String s)
        //判断某字符串是否以某子字符串开始
        System.out.println("this is a goode man".startsWith("this"));//ture

        //String subString(int beginIndex)
        //截取字符串
        System.out.println("http:www.baidu.com".substring(5,8));//www
        System.out.println("http:www.baidu.com".substring(5));//www.baidu.com

        //String toLowerCase()
        //转化为小写
        String s = "ABC".toLowerCase();
        System.out.println(s);//abc
        //String toUpperCase()
        //转化为大写
        System.out.println("abc".toUpperCase());//ABC

        //String trim()
        //去除前后空白
        System.out.println(" zhangsan  ".trim());//zhangsan

        //static String  valueOf()
        //String中的唯一一个静态方法,将非字符串转化为字符串
        String s7 = String.valueOf(1);
        System.out.println(s7);//1
        String s8 = String.valueOf(false);
        System.out.println(s8);//false
        String s9 = String.valueOf(2.3);
        System.out.println(s9);//2.3


    }
}

你可能感兴趣的:(javase,java)