Java基础学习day15

字符串

一、字符串

1、定义 :简单理解:用一根签将若干个字符串起来的串叫做字符串。
由多个字符组成的一串数据叫做字符串,也可以看作是一个字符数组。

    String:(去看API)
    String类代表字符串。
    Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。
    字符串不变,它们的值在创建后不能被更改。
    字符串缓冲区支持可变字符串。
    因为String对象是不可变的,它们可以被共享。

注意:
字符串一旦被创建,它的值就不能被改变,指的是在常量池中的值不能被改变,而不是栈中的引用不能被改变.

二、String类的方法

1、构造方法

        public String()初始化新创建的String对象,使其表示空字符序列。
        public String(byte[] bytes)
        public String(byte[] bytes,int offset,int length)
        public String(char[] value)
        public String(char[] value,int offset,int count)
        public String(String original)

2、判断功能

        boolean equals(Object obj)
        boolean equalsIgnoreCase(String str)
        boolean contains(String str)
        boolean startsWith(String str)
        boolean endsWith(String str)
        boolean isEmpty()
public class StringDemo7 {
    public static void main(String[] args) {
        //创建两个字符串对象
        String s1 = "hello";
        String s2 = "world";

        //public boolean equals(Object anObject)将此字符串与指定对象进行比较。
        // 其结果是true当且仅当该参数不是null并且是String对象,表示相同的字符序列作为该对象。
        System.out.println(s1.equals(s2));
        System.out.println("================================================");
        //public boolean equalsIgnoreCase(String anotherString)将此String与其他String比较,忽略案例注意事项。
        // 如果两个字符串的长度相同,并且两个字符串中的相应字符等于忽略大小写,则两个字符串被认为是相等的。
        String s3 = "heLLO";
//        System.out.println(s1.equals(s3));
        System.out.println(s1.equalsIgnoreCase(s3));
        System.out.println("================================================");
        //public boolean contains(CharSequence s)当且仅当此字符串包含指定的char值序列时才返回true。
        //判断字符串中是否包含小字符串
        System.out.println(s1.contains("ll"));
        System.out.println(s1.contains("ho"));
        System.out.println("=================================================");
        //public boolean startsWith(String s)测试此字符串是否以指定的前缀开头。
        //判断大字符串是否以某小字符串开头
        System.out.println(s1.startsWith("lo"));
        System.out.println(s1.startsWith("h"));
        System.out.println(s1.startsWith("hel"));
        System.out.println("=================================================");
        //boolean endsWith(String str)
        //判断大字符串是否以某小字符串结尾
        System.out.println(s1.endsWith("w"));
        System.out.println(s1.endsWith("ll"));
        System.out.println(s1.endsWith("lo"));
        System.out.println("=================================================");
        //boolean isEmpty()
        //判断字符串是否为空
//        System.out.println(s1.isEmpty());
        String s = "";
        String s4 = null;
//        System.out.println(s.isEmpty());
//        System.out.println(s4.isEmpty()); //NullPointerException

        System.out.println("======================================================");
        //需求:比较s与是s1的值是否相同
        System.out.println(s1.equals(s));
        //需求2:判断s1的内容是否是"bigdata"
        System.out.println(s1.equals("bigdata"));
        s1 = null;
//        System.out.println(s1.equals("bigdata")); //NullPointerException
        //解决方案1:今后比较两个字符串内容是否相同的时候,把确定的放在前面调用方法,避免发生空指针异常(优先推荐第一种)
        System.out.println("bigdata".equals(s1));

        //解决方案2:在比较之前,做一次Null判断
        if (s1 != null) {
            System.out.println(s1.equals("bigdata"));
        }else {
            System.out.println("字符串s1是为null");
        }


    }
}

3、获取功能

    int length()
    char charAt(int index)
    int indexOf(int ch)
    int indexOf(String str)
    int indexOf(int ch,int fromIndex)
    int indexOf(String str,int fromIndex)
    String substring(int start)
    String substring(int start,int end)
public class StringDemo8 {
    public static void main(String[] args) {
        String s = "shujiakeji123";

        //public int length()返回此字符串的长度。 长度等于字符串中的数字Unicode code units 。
        System.out.println(s.length()); //13
        System.out.println("======================================");
        //public char charAt(int index)返回char指定索引处的值。 指数范围为0至length() - 1 。
        // 该序列的第一个char值在索引0 ,下一个索引为1 ,依此类推,与数组索引一样。
        System.out.println(s.charAt(3));
        System.out.println(s.charAt(12));
//        System.out.println(s.charAt(13)); //IndexOutOfBoundsException - 如果 index参数为负数或不小于此字符串的长度。
        System.out.println("=======================================");
        //public int indexOf(int ch)返回指定字符第一次出现的字符串内的索引。
//        System.out.println(s.indexOf(97));
//        System.out.println(s.indexOf('a'));
        System.out.println(s.indexOf('z')); //如果此字符串中没有此类字符,则返回-1
        System.out.println("=======================================");
        //public int indexOf(String str)返回指定子字符串第一次出现的字符串内的索引。
        System.out.println(s.indexOf("ake")); // 返回的是字符串首字母出现在字符串中的位置
        System.out.println(s.indexOf("spark")); //如果整个字符串在大字符串中不存在,返回-1
        System.out.println("=======================================");
        //public int indexOf(int ch,int fromIndex)返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。
        System.out.println(s.indexOf(97,6)); //如果不发生的字符。-1
        System.out.println(s.indexOf(97,3)); //如果找到返回的是在整个字符串中的位置索引
        System.out.println("=======================================");
        //int indexOf(String str,int fromIndex)(自主学习)
        System.out.println("=======================================");
        //public String substring(int beginIndex)返回一个字符串,该字符串是此字符串的子字符串。
        // 子字符串以指定索引处的字符开头,并扩展到该字符串的末尾。
        //做字符串截取的
        System.out.println(s.substring(4)); //包含开始的位置,一直截取到末尾
        System.out.println("========================================");
        //String substring(int start,int end) 含头不含尾 [start,end)
        System.out.println(s.substring(4,13));



    }
}

4、 String类的转换功能:

        byte[] getBytes()
        char[] toCharArray()
        static String valueOf(char[] chs)
        static String valueOf(int i)
        String toLowerCase()
        String toUpperCase()
        String concat(String str)
public class StringDemo9 {
    public static void main(String[] args) {
        String s = "bigDataSPArk";

        //public byte[] getBytes()使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中。
        //将字符串转成字节数组
        byte[] bytes = s.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        System.out.println("=====================================================");
        //char[] toCharArray()
        // 字符串 ---> 字符数组
        //将字符串转成一个字符数组
        char[] chars = s.toCharArray();
        for(int i = 0;i<chars.length;i++){
            if(i==0){
                System.out.print("["+chars[i]+",");
            }else if(i==chars.length-1){
                System.out.print(chars[i]+"]\n");
            }else {
                System.out.print(chars[i]+",");
            }
        }
        System.out.println("========================================================");
        //static String valueOf(char[] chs)
        // 字符数组 ---> 字符串
        //将字符数组转成一个字符串
        String s1 = String.valueOf(chars);
        System.out.println(s1);
        System.out.println("=========================================================");
        //static String valueOf(int i)
        // 100  /  "100"
        String s2 = String.valueOf(100);
        System.out.println(s2);
        System.out.println("==========================================================");
        //String toLowerCase()  将字符串中字符全部转小写 针对于26个字母有效
        String s3 = s.toLowerCase();
        System.out.println(s3);
//        s = "bigDataSPArk我;";
//        String s4 = s.toLowerCase();
//        System.out.println(s4);
        System.out.println("===========================================================");
        //String toUpperCase()
        String s4 = s.toUpperCase();
        System.out.println(s4);
        System.out.println("===========================================================");
        //String concat(String str)  用于字符串拼接
        //需求:现在想在原先的字符串后面拼接上 "今天天气不好"
//        s += "今天天气不好";
//        System.out.println(s);
        System.out.println(s.concat("今天天气不好"));


    }
}

5、 替换功能

        String replace(char old,char new)
        String replace(String old,String new)
    去除字符串两空格
        String trim()
    按字典顺序比较两个字符串
        int compareTo(String str)
        int compareToIgnoreCase(String str)
public class StringDemo10 {
    public static void main(String[] args) {
        String s = "hello World Java BIGData trorld";

        //public String replace(char oldChar,char newChar)
        // 返回从替换所有出现的导致一个字符串oldChar在此字符串newChar 。
        // 替换字符串中所有的要被替换的字符,返回的是替换后的字符串
        String s1 = s.replace('a', 'q');
        System.out.println(s1);
        //如果被替换的字符在原字符串中不存在,返回的是原来的字符串
        String s2 = s.replace('z', 'p');
        System.out.println(s2);
        System.out.println("==============================================");
        //String replace(String old,String new)
        //替换字符串中所有的要被替换的字符串,返回的是替换后的字符串
//        String s3 = s.replace("orld", "qwer");
//        System.out.println(s3);
//        String s3 = s.replace("orld", "qwerdf");
//        System.out.println(s3);
        //如果被替换的字符串在原字符串中不存在,返回的是原来的字符串
        String s3 = s.replace("qwer", "pppppp");
        System.out.println(s3);
        System.out.println("================================================");
        //String trim() 去除字符串两边的所有空格
        String s4 = " hello World Java BIGData trorld  ";
        System.out.println("去除空格之前:");
        System.out.println(s4);
        String s5 = s4.trim();
        System.out.println("去除空格之后:");
        System.out.println(s5);
        System.out.println("===============================================");
        //public int compareTo(String anotherString)按字典顺序比较两个字符串。
//        String s6 = "hello"; //h的ASCII码值104
//        String s7 = "world"; //w的ASCII码值119
//        System.out.println(s6.compareTo(s7));

        String s6 = "hello";
        String s7 = "hel";
        System.out.println(s6.compareTo(s7));


    }
}

(笔记由老师上课内容所制)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值