字符串相关的常用类

字符串的创建(6种方法)

1、String() 初始化一个新创建的 String 对象,使其表示一个空字符序列。注意,由于 String 是不可变的,所以无需使用此构造方法。

String s = new String();

2、String(String original) 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。

String s2 = new String("abc");

3、String(char[] value) 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。

char[] chars = {'a','b','c'};
String s3 = new String(chars);

4、String(byte[] bytes) 通过使用平台的默认字符集UTF-8解码指定的 byte 数组,构造一个新的 String。

5、String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。

// 编码集UTF-8:一个汉字占3个字节,一个字母占1个字节
byte[] bytes1 = "我Java".getBytes();  // 默认编码集UTF-8转为字节表示形式,转为10个字节(3+3+4)
System.out.println(Arrays.toString(bytes1));// [-26, -120, -111, -25, -120, -79, 74, 97, 118, 97]
String s4 = new String(bytes1); // 解码操作,默认UTF-8
System.out.println(s4); // 我爱Java
// 编码集GBK:一个汉字占2个字节,一个字母占1个字节 byte[] bytes2 = "我Java".getBytes("GBK");// 指定编码集GBK转为字节表示形式,转为8个字节(2+2+4) System.out.println(Arrays.toString(bytes2));// [-50, -46, -80, -82, 74, 97, 118, 97] String s5 = new String(bytes2,"GBK"); // 解码操作,指定GBK System.out.println(s5); // 我爱Java

6、" " 直接双引号引起来的,也是一个字符串对象。

String s6 = "abc";

----------------------------------------------------------------

字符串是常量 -> 它们的值在创建之后不能更改。

  解释:字符串底层是一个被 final 修饰的数组,所以字符串值在创建后不能改变,同时数组本身不能在自己上进行扩容;字符串拼接即是产生了一个新数组。

字符串缓冲区 -> 字符串缓冲区支持可变的字符串。

字符串常量池 -> 因为 String 对象是不可变的,所以可以共享。

字符串常量池作用:减少内存开销,让字符串对象得到复用。

字符串常量池的位置:在方法区中。

什么样的字符串会放入常量池?

 1、直接使用 " " 引用的字符串对象会被放入到常量池中,如:String s = "abc";

 2、" " + " " 拼接,最终被编译器转为 " " ,也会被放入到常量池中,如:String str = "ab" + "c";

-------------------------------------------------------------------------------

String 类相关Api

import java.util.Arrays;

public class stringTest {
    public static void main(String[] args) {
        //1.int length() 返回字符串长度
        String s = "hello";
        System.out.println(s.length());

        //2.char charAt(int index) 返回指定索引处的char值。
        String s1 = "helloworld";
        System.out.println(s1.charAt(5));// w
        //应用场景:字符串遍历
        String s2 = "world";
        for (int i = 0; i < s2.length(); i++) {
            System.out.print(s2.charAt(i) + "\t");
        }   // w    o    r    l    d

        //3.int compareTo(String anotherString) 按字典顺序比较两个字符串。
        String sa = "abc";
        String sb = "bbc";
        String sc = "aba";
        String sd = "c";
        //比较首位 a-b = -1
        System.out.println(sa.compareTo(sb)); // -1
        //前两位相同,比较第三位 c-a = 2
        System.out.println(sa.compareTo(sc)); // 2
        // abc - a = 2(多两个)
        System.out.println(sc.compareTo(sd)); // 2


        //4.boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时,返回true
        String s4 = "helloworld";
        String s4c = "owo";
        System.out.println(s4.contains(s4c)); //true

        //5.boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始
        String s5 = "www.lwj.com";
        System.out.println(s5.startsWith("www")); //true

        //6.boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束
        String s6 = "123@163.com";
        System.out.println(s6.endsWith("@163.com")); //true

        //7.boolean equals(Object anObject) 将此字符串与指定的对象比较。
        //String类继承Object类,重写了该方法,比较内容是否相同
        String s7 = "我爱Java";
        System.out.println(s7.equals("我爱Java")); //true

        //8.boolean equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写
        //应用场景:验证码(不分大小)
        System.out.println("4RqB".equalsIgnoreCase("4rqB")); //true


        //9.boolean isEmpty() 当且仅当 length()为0时返回 true
        System.out.println("".isEmpty()); //true
        System.out.println("abc".isEmpty()); //false
        String s9 = new String();
        System.out.println(s9.isEmpty()); //true

        //10.int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引
        System.out.println("hello".indexOf("e")); //1
        System.out.println("hello".indexOf("lo")); //3
        System.out.println("hello".indexOf("a")); //-1(表示没有)

        //11.int lastIndexOf(Strin g str) 返回指定子字符串在此字符串中最右边出现处的索引
        System.out.println("helloworld".lastIndexOf("l")); //8
        System.out.println("helloworld".lastIndexOf("ll")); //2

        //12.String trim() 返回字符串的副本,忽略前导空白和尾部空白
        //应用场景:去除用户输入内容前后的空字符
        System.out.println("  hel lo  ".trim()); //hel lo

        //13.String replace(char oldChar, char newChar) 字符替换
        //应用场景:比如骂人敏感词替换成***
        String s13 = "我爱Java";
        String  replace = s13.replace('a','A');
        System.out.println(replace); //我爱JAvA

        //14.String replaceAll(String regex, String replacement) 字符替换
        String s14 = "我爱Java123,Java学习111";
        System.out.println(s14.replaceAll("Java","编程")); //我爱编程,编程学习
        //正则表达式的替换,比如("\\d", "*")把所有数字换成*
        System.out.println(s14.replaceAll("\\d","*")); //我爱Java***,Java学习***

        //15.String[] split(String regex) 指定拆分
        String s15 = "张三//18//广州天河区";
        String[] split = s15.split("//");
        System.out.println(Arrays.toString(split)); //[张三, 18, 广州天河区]

        //16.String substring(int beginIndex) 截取子字符串
        System.out.println("unhappy".substring(2)); //happy

        //17.String substring(int beginIndex, int endIndex) 截取子字符串(含头不含尾)
        System.out.println("unhappy".substring(2,4)); //ha

        //18.char[] toCharArray() 字符串 -> 字符数组
        char[] chars = "helloworld".toCharArray();
        System.out.println(Arrays.toString(chars)); //[h, e, l, l, o, w, o, r, l, d]

        //19.String toUpperCase() 全部转大写
        System.out.println("hello".toUpperCase()); //HELLO

        //20.String toLowerCase() 全部转小写
        System.out.println("HELLO".toLowerCase()); //hello

        //21.static String valueOf(类型 参数名) 其它类型 -> 字符串
        //其它类型:整型,布尔,浮点型,Object,字符型char,字符数组
        //该方法相当于:"" + 任意类型 -> 字符串
        String s21 = String.valueOf(10);
        System.out.println(s21); //10
        System.out.println(String.valueOf(true)); //true
        System.out.println(String.valueOf('a')); //a
        char[] chars1 = {'a','b','c'};
        System.out.println(String.valueOf(chars1)); //abc
        System.out.println(String.valueOf(new String())); //
    }
}

 

posted @ 2022-04-09 00:08  鹿先森JIAN  阅读(92)  评论(0编辑  收藏  举报