String类

String类

字符串就是字符的集合。字符串是常量,一旦被创建,就不能被改变。

常见构造方法:

  1. public String():空构造

  2. public String(byte[] bytes):把字节数组转成字符串

  3. public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)

    例:

    public class test {
        public static void main(String[] args) {
            String s = "我爱你";
            //可以通过String.getBates方法返回一个bate类型的数组,获取字符串的字节数组
            byte[] bytes = s.getBytes();
            //通过循环打印
            for (int i = 0; i < bytes.length; i++) {
                System.out.println(bytes[i]);
            }
            System.out.println("==================================");
    
            byte[] bytes1 = {-26, -120, -111, -25, -120, -79, -28, -67, -96};
            //通过public String(byte[] bytes)把字节数组bytes1转成字符串
            String s1 = new String(bytes1);
            System.out.println(s1);                   //打印  我爱你
            System.out.println("===================================");
            //通过public String(byte[] bytes,int index,int length)把字节数组的一部分转成字符串
            //可能遇到的错误:StringIndexOutOfBoundsException,字符数组索引越界
            String s2 = new String(bytes1, 3, 6);
            System.out.println(s2);       //打印   爱你
        }
    }
    
  4. public String(char[] value):把字符数组转成字符串

  5. public String(char[] value,int index,int count):把字符数组的一部分转成字符串

    例:

    public class test2 {
        public static void main(String[] args) {
            char[] chars = {'我', '爱', '你'};
            //通过public String(char[] value)把字符数组转成字符串
            String s = new String(chars);
            System.out.println(s);//打印  我爱你
            System.out.println("======================================");
            //通过public String(char[] value,int index,int count),把字符数组的一部分转成字符串
            String s1 = new String(chars, 1, 2);
            System.out.println(s1);//打印  爱你
        }
    }
    
    
  6. public String(String original):把字符串常量值转成字符串

String的特点:

一旦被创建就不能改变 因为字符串的值是在堆内存的常量池中划分空间 分配地址值的。

例1:String s = new String(“hello”)和String s = “hello”;的区别

简易图解:

String类的判断功能

  1. public boolean equals(Object obj)

    比较字符串的内容是否相同,区分大小写

  2. public boolean equalsIgnoreCase(String str)

    比较字符串的内容是否相同,忽略大小写

  3. public boolean contains(String str)

    判断字符串中是否包含传递进来的字符串

  4. public boolean startsWith(String str)

    判断字符串是否以传递进来的字符串开头

  5. public boolean endsWith(String str)

    判断字符串是否以传递进来的字符串结尾

  6. public boolean isEmpty()

    判断字符串的内容是否为空串""

例:在这里插入图片描述

public class test4 {
    public static void main(String[] args) {
        String s1 = "I Love You";
        String s2 = "I Love You";
        String s3 = "i love you";

        // 1. 通过public boolean equals(Object obj),比较字符串的内容是否相同,区分大小写
        System.out.println(s1.equals(s2));// true
        System.out.println(s1.equals(s3));// false
        System.out.println("=======================================");
        // 2. 通过public boolean equalsIgnoreCase(String str),比较字符串的内容是否相同,忽略大小写
        System.out.println(s1.equalsIgnoreCase(s3));// true
        System.out.println("=======================================");
        // 3. 通过public boolean contains(String str),判断字符串中是否包含传递进来的字符串
        System.out.println(s1.contains("Love"));// true
        System.out.println(s1.contains("love"));// false
        System.out.println("=======================================");
        // 4. 通过public boolean startsWith(String str),判断字符串是否以传递进来的字符串开头
        System.out.println(s1.startsWith("I"));// true
        System.out.println(s1.startsWith("i"));// false
        System.out.println("=======================================");
        // 5. public boolean endsWith(String str),判断字符串是否以传递进来的字符串结尾
        System.out.println(s1.endsWith("You"));// true
        System.out.println(s1.endsWith("you"));// false
        System.out.println("=======================================");
        // 6. 通过public boolean isEmpty(),判断字符串的内容是否为空串""。
        System.out.println(s1.isEmpty());// false
        System.out.println("".isEmpty());// true
    }
}

String类的获取功能

  1. public int length() 获取字符串的长度

  2. public char charAt(int index) 获取指定索引位置的字符

  3. public int indexOf(char ch) 返回指定字符在此字符串中第一次出现处的索引

  4. public int indexOf(String str) 返回指定字符串在此字符串中第一次出现处的索引

  5. public int indexOf(int ch,int fromIndex) 返回指定字符在此字符串中从指定位置后第一次出现处的索引

  6. public int indexOf(String str,int fromIndex) 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

  7. public String substring(int start) 从指定位置开始截取字符串,默认到末尾。

  8. public String substring(int start,int end) 从指定位置开始到指定位置结束截取字符串。

    例:

    public class test5 {
        public static void main(String[] args) {
            String s = "我爱你,爱你,爱你,爱你";
            // 1. public int length() 获取字符串的长度
            System.out.println(s.length());// 12
            // 2. public char charAt(int index) 获取指定索引位置的字符
            System.out.println(s.charAt(1));// 爱
            // 3. public int indexOf(char ch) 返回指定字符在此字符串中第一次出现处的索引
            System.out.println(s.indexOf('爱'));// 1
            // 4. public int indexOf(String str) 返回指定字符串在此字符串中第一次出现处的索引
            System.out.println(s.indexOf("爱你"));// 1
            // 5. public int indexOf(int ch,int fromIndex) 返回指定字符在此字符串中从指定位置后第一次出现处的索引
            System.out.println(s.indexOf('你', 5));// 5
            // 6. public int indexOf(String str,int fromIndex) 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
            System.out.println(s.indexOf("爱你", 3));// 4
            // 7. public String substring(int start) 从指定位置开始截取字符串,默认到末尾。
            System.out.println(s.substring(4));// 爱你,爱你,爱你
            // 8. public String substring(int start,int end) 从指定位置开始到指定位置结束截取字符串。
            System.out.println(s.substring(2, 5));// 你,爱
        }
    }
    

String类的转换功能

  1. public byte[] getBytes() 把字符串转换为字节数组;

  2. public char[] toCharArray() 把字符串转换为字符数组;

  3. public static String valueOf(char[] chs) 把字符数组转成字符串;

  4. public static String valueOf(int i) 把int类型的数据转成字符串;

    注意:String类的valueOf方法可以把任意类型的数据转成字符串

  5. public String toLowerCase() 把字符串转成小写;

  6. public String toUpperCase() 把字符串转成大写;

  7. public String concat(String str) 把字符串拼接;

String类的其他功能

  1. public String replace(char old,char new) 将指定字符进行互换
  2. public String replace(String old,String new) 将指定字符串进行互换
  3. public String trim() 去除两端空格
  4. public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果,如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果,如果连个字符串一摸一样 返回的就是0
    ace(String old,String new) 将指定字符串进行互换
  5. public int compareToIgnoreCase(String str) 跟4.一样 只是忽略大小写的比较
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值