JAVA-------JAVA中String类及其常用功能

String类

String类

不可变的字符序列

String代表字符串,字符串是由多个字符组成的一串数据,字符串可以看成字符数组,
1.字符串字面值“abc”也可以看成一个字符串的对象
2.字符串是常量,一旦被创建,就不能改变
3.字符串可以看做是一个长度固定的有序字符序列,每个组成的字符编有索引从0开始

常见构造方法
  • public String():空构造
  • public String(byte[] bytes):把字节数组转成字符串
  • public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
  • public String(char[] value):把字符数组转成字符串
  • public String(char[] value,int index,int count):把字符数组的一部分转成字符串
  • public String(String original):把字符串常量值转成字符串
public class Test {
    public static void main(String[] args) {
        byte[] b={97,98,99,100,101};
        String s = new String(b);
        System.out.println(s);
        String s1 = new String(b, 0, 3);
        System.out.println(s1);
    }
}

输出结果为

abcde
abc

public class Test {
    public static void main(String[] args) {
        char [] c={'a','b','c','d','e'};
        String s = new String();
        for (int i = 0; i <c.length; i++) {
            s+=c[i];
        }
        System.out.println(s);
        String s1 = new String(c);
        System.out.println(s1);
        String s2 = new String(c, 3, 2);
        System.out.println(s2);
    }
}

输出结果为

abcde
abcde
de
判断功能
  • public boolean equals(Object obj); 比较字符串的内容是否相同,区分大小写
  • public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
  • public boolean contains(String str); 判断字符串中是否包含传递进来的字符串
  • public boolean startsWith(String str); 判断字符串是否以传递进来的字符串开头
  • public boolean endsWith(String str); 判断字符串是否以传递进来的字符串结尾
  • public boolean isEmpty(); 判断字符串的内容是否为空串""
public class Test {
    public static void main(String[] args) {
        boolean b1="abc".equals("abc");
        boolean b2="abc".equalsIgnoreCase("ABC");
        String b3="";
        boolean empty=b3.isEmpty();
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(empty);
        System.out.println("abc".contains("bc"));
        System.out.println("abc".startsWith("a"));
        System.out.println("abc".endsWith("c"));
    }
}

输出结果

皆为 true
获取功能
  • public int length(); 获取字符串的长度。
  • public char charAt(int index); 获取指定索引位置的字符
  • public int indexOf(int ch); 返回指定字符在此字符串中第一次出现处的索引。
  • public int indexOf(String str); 返回指定字符串在此字符串中第一次出现处的索引。
  • public int indexOf(int ch,int fromIndex); 返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  • public int indexOf(String str,int fromIndex); 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  • public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
  • public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
public class Test {
    public static void main(String[] args) {
        
        System.out.println("abcde".length());
        
        String s="abcde";
        char ch = s.charAt(3);
        System.out.println(ch);
        
        int index = s.indexOf('b');
        System.out.println(index);
        
        String s1="babcde";
        int index1 = s1.indexOf("b", 1);
        System.out.println(index1);
        
        String substring = s1.substring(1);
        System.out.println(substring);
        
        String substring1 = s1.substring(1, 3);
        System.out.println(substring1);
    }
}

输出结果为:

5
d
1
2
abcde
ab
转换功能
  • public byte[] getBytes(): 把字符串转换为字节数组。
  • public String(byte[] bytes): 把字节数组转成字符串
  • public char[] toCharArray(): 把字符串转换为字符数组。
  • public String toLowerCase(): 把字符串转成小写。
  • public String toUpperCase(): 把字符串转成大写。
  • public static String valueOf(char[] chs): 把字符数组转成字符串。
  • public static String valueOf(int i): 把int类型的数据转成字符串。
    注意:String类的valueOf方法可以把任意类型的数据转成字符串。
  • public String concat(String str): 把字符串拼接。
public class Test {
    public static void main(String[] args) {
        String str = "说再见";
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        System.out.println("--------------------------");

        String s = new String(bytes);
        System.out.println(s);

        String str2 = "说再见";
        char[] chars = str2.toCharArray();
        System.out.println(chars);

        String s2 = "abc".toUpperCase();
        String s3 = "BcD".toLowerCase();
        System.out.println(s2);
        System.out.println(s3);

        String s4 = String.valueOf(100);
        String s5 = String.valueOf(false);
        System.out.println(s4);
        System.out.println(s5);

        char[] chars1={'a','b','c'};
        String s6 = new String(chars1);
        String s7 = String.valueOf(chars);
        System.out.println(s6);
        System.out.println(s7);

        String abc = "abc".concat("ABC").concat("Abc").concat("123");
        System.out.println(abc);

    }
}

输出结果为:

-24
-81
-76      //这三个数字表示 说
-27
-122
-115	//这三个数字表示 再
-24
-89
-127	//这三个数字表示 见
--------------------------
说再见
说再见
ABC
bcd
100
false
abc
说再见
abcABCAbc123

Process finished with exit code 0

替换功能
  • public String replace(char old,char new) 将指定字符进行互换
  • public String replace(String old,String new) 将指定字符串进行互换
public class Test {
    public static void main(String[] args) {
        String r="apcde".replace("p","b");
        String s="走吗".replace("吗","吧");
        System.out.println(r);
        System.out.println(s);
    }
}

输出结果为:

abcde
走吧
去除字符串两端空格
  • public String trim() 去除两端空格
public class Test {
    public static void main(String[] args) {
        String r="    学   习      ";
        String t = r.trim();
        System.out.println(t);
    }
}

输出结果为:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值