字符串

1.概述

java.long.String 类代表字符串

API中:java程序中的所有字符串面值(如“ABC”)都作为此类的实例实现,其实就是说,程序当中所有的双引号字符串都是String类的对象。(就算没有new,也是)

字符串是常量

特点:

1.字符串的内容永不改变

2.正是因为字符串不可改变,所以字符串是可以共享使用的。

3.字符串效果相当于是char[]字符数组,但是底层原理是byte[]字节数组

2.字符串的创建

三种构造方法:

public String();  创建一个空白字符串,不含任何内容

public String(char[] array);  根据字符数组的内容,来创建对应的字符串

public String(byte[] array);  根据字节数组的内容,来创建对应的字符串

一种直接创建:

3.字符串的常量池(堆中)

程序当中直接写上的双引号字符串,就在字符串常量池中

内存图:

 

 4.字符串的比较

==是进行对象的地址比较,如果确实需要字符串的内容比较,可以使用两个方法:public boolean equals(Object obj):参数可以是任何对象,知识参数是一个字符串并且内容相同才会给true,否则返回false

注意:1.任何对象都有object进行比较

2.equals方法具有对称性,也就是a.equals(b)和b.equals(a)一样

3.如果双方一个常量一个变量,推荐把常量字符串写在前面,如 abc.equas(str);

 

public boolean equalsIgnoreCase(String str) 忽略大小写进行内容比较

5.字符串的获取

1.public int length(); 获取字符串当中字符个数,拿到字符串长度

2.public String concat(); 拼接两个字符串

3.public char charAt(int index); 获取指定索引位置的单个字符串,索引从0开始

4.piblic int indexOf(String str); 查找参数字符串在本字符串中首次出现的位置,如果没有返回-1值

public class Demo02StringGet {
    public static void main(String[] args) {
        //获取字符串的长度
        int length = "vgbjhnkdmbgvbhbgfs".length();
        System.out.println("字符串长度是:"+length);
        //拼接字符串
        String str1 = "hello";
        String str2 = "World";
        String str3 = str1.concat(str2);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        //获取指定索引位置的单个字符、
        char ch = "Hello".charAt(1);
        System.out.println("在一号未知的字符是:"+ch);
        System.out.println("========================");

        //查找参数字符串在本来字符串当中出现的第一次索引位置
        //如果没有,返回-1
        String original = "HelloWorldHelloWorldHelloWorld";
        int index = original.indexOf("llo");
        System.out.println("索引值:"+index);
        System.out.println();
    }
}

6.字符串的截取

public String sbustring(int index);机取钱从参数位置一直到末尾,返回新字符串
public String substring(int begin,int end); 截取从begin开始一直到end结束中间的字符串
备注:[begin,end)包含左边,不包含右边
public class Demo03SubString {
    public static void main(String[] args) {
        String str1 = "HelloWorld";
        String str2 = str1.substring(5);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println("==============");

        String str3 = str1.substring(4,7);
        System.out.println(str3);
    }
}

 

7.字符串的替换

String中与转换相关的方法有:
public char[] toCharArray(); 将当前的字符串拆分为字符数组作为返回值
public byte[] getBytes(); 获得当期字符串的底层字节数组
public String replace(CharSequence oldString, CharSequence newString);
将所有出现的老字符串替换成为新的字符串,返回替换之后的新字符串
备注:CharSequence意思是可以接受字符串类型,你就把它当做字符串
public class Demo04StringConvert {
    public static void main(String[] args) {
        //转化为数组
        char[] chars = "Hello".toCharArray();
        System.out.println(chars[0]);
        System.out.println(chars.length);
        //转化为字节数组(以后学了io流就能用的上)
        byte[] bytes = "abc".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        //凡是有o的地方都替换为*
        String str1 = "How do you do?";
        String str2 = str1.replace("o", "*");
        System.out.println(str1);
        System.out.println(str2);

        String lang1 = "会不会玩呀,你大爷的!你大爷的!你大爷的!你大爷的!你大爷的!!";
        String lang2 = lang1.replace("你大爷的", "****");
        System.out.println(lang2);
    }
}

8.字符串的分割

分割字符串的方法:
public String split(String regex); 安照参数的规则,将字符串切分为若干部分
split方法其实是一个正则表达式,如果按照英文"."进行分割,一定写成"\\."
public class Demo05StringSplit {
    public static void main(String[] args) {
        String str1 = "aaa,bbb,ccc";
        String[] array1 = str1.split(",");
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }
        System.out.println("============");
        String str2 = "aaa.bbb.ccc";
        String[] array2 = str2.split("\\.");
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
        }
    }
}

 

posted @ 2020-02-18 11:13  佥刂同学  阅读(154)  评论(0)    收藏  举报