Java-String类

String常用类

  • 字符串是由多个字符组成的一串数据(字符序列),字符串可以看成是字符数组。

  • 常见的构造方法

public class Test2 {

    public static void main(String[] args) {

//      A:常见构造方法
//      public String():空构造

//      public String(byte[] bytes):把字节数组转成字符串  
        byte[] bys=new byte[]{98,99,100,65,68,66};
        String string = new String(bys);
        System.out.println(string);//bcdADB

//      public String(byte[] bytes,int index,int length)
//      把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
        String string2 = new String(bys,0,3);
        System.out.println(string2);//bcd
        int length = string.length();
        System.out.println(length);//6

//      public String(char[] value):把字符数s组转成字符串
//      public String(char[] value,int index,int count):把字符数组的一部分转成字符串
        char[] ch={'我','很','好'};
        String string3 = new String(ch);
        System.out.println(string3);//我很好
        String string4 = new String(ch,0,1);
        System.out.println(string4);///

    }

}
  • String类的判断功能
public class Test3 {

    public static void main(String[] args) {

        // public boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
        String s1 = "ABC";
        String s2 = "abC";
        boolean b = s1.equals(s2);
        System.out.println(b);//false

        // public boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
        boolean c = s1.equalsIgnoreCase(s2);
        System.out.println(c);//true

        // public boolean contains(String str): 判断字符串中是否包含传递进来的字符串

        String str = "爱生活生活生活爱javajavajava";

        boolean contains = str.contains("java");
        if (contains) {
            String str2 = str.replace("java", "c");
            System.out.println(str2);
        }//爱生活生活生活爱ccc

        // public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
        String s3 = "abcddfdfd";
        boolean with = s3.startsWith("ab");
        System.out.println(with);//true

        // public boolean endsWith(String str):判断字符串是否以传递进来的字符串结尾
        boolean with2 = s3.endsWith("d");
        System.out.println(with2);//true

        // public boolean isEmpty(): 判断字符串的内容是否为空串""
        String s9 = "";
        boolean empty = s9.isEmpty();
        System.out.println(empty);//true

    }

}
  • String类的获取功能
public class Test1 {

    public static void main(String[] args) {
        // A:String类的获取功能   

        // public int length():获取字符串的长度。
        int length = "helloworld!".length();
        System.out.println(length);//11

        // public char charAt(int index):获取指定索引位置的字符
        // public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
        // public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
        String str="你好啊世界世界世界世界你好啊";
        char charAt = str.charAt(2);
        System.out.println(charAt);//啊
        int indexOf = str.indexOf('世');
        System.out.println(indexOf);//3
        int indexOf2 = str.indexOf("世界");
        System.out.println(indexOf2);//3 只能查看第一个字出现的位置

        // public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
        String string="abcdabcdabcdefghljklmn";
        int indexOf3 = string.indexOf("k", string.length()/2);
        System.out.println(indexOf3);//18

        // public String substring(int start):从指定位置开始截取字符串,默认到末尾。
        // public String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
        String string2="爱生活爱Java";
        String substring = string2.substring(string2.indexOf("生"));
        System.out.println(substring);//生活爱Java

        String substring2 = string2.substring(0, 3);//包含头不包含尾
        System.out.println(substring2);//爱生活
    }

}
  • String类的转化功能
public class Test4 {

    public static void main(String[] args) {

        // public byte[] getBytes():把字符串转换为字节数组。
        byte[] bytes = "狗年大吉,阖家欢乐".getBytes();
        String string = new String(bytes);
        System.out.println(string);//狗年大吉,阖家欢乐

        // public char[] toCharArray():把字符串转换为字符数组。
        char[] charArray = "新年快乐".toCharArray();
        System.out.println(charArray[0]);//新
        String string2 = new String(charArray);
        System.out.println(string2);//新年快乐

        // public static String valueOf(char[] chs):把字符数组转成字符串。
        // public static String valueOf(int i):把int类型的数据转成字符串。
        // 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
        String valueOf = String.valueOf(charArray);
        System.out.println(valueOf);//新年快乐
        String valueOf2 = String.valueOf(false);
        System.out.println(valueOf2);// "false"
        String valueOf3 = String.valueOf(123);
        System.out.println(valueOf3);// "123"
        int parseInt = Integer.parseInt("123");
        System.out.println(parseInt + 1);//124


        // public String toLowerCase():把字符串转成小写。
        // public String toUpperCase():把字符串转成大写。
        String lowerCase = "ABCD".toLowerCase();
        System.out.println(lowerCase);//abcd
        String upperCase = "abcd".toUpperCase();
        System.out.println(upperCase);//ABCD


        // public String concat(String str):把字符串拼接。
        String concat = "狗".concat("年").concat("大").concat("吉").concat("!");
        System.out.println(concat);//狗年大吉!
    }

}
  • String类的替换功能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的String类是一个非常重要的类,它代表了字符串类型的数据。以下是关于Java中String类的一些介绍和操作方法: 1. String类是Java中的一个类,用于表示字符串类型的数据。 2. String类的对象是不可变的,也就是说,一旦创建了一个String对象,就不能再修改它的值。 3. 可以使用双引号来创建一个String对象,例如:String str = "Hello World"。 4. String类中有很多方法可以用来操作字符串,例如获取字符串长度、比较字符串、查找子字符串等等。 5. 获取字符串长度的方法是:str.length(),其中str是一个String对象。 6. 比较两个字符串是否相等的方法是:str1.equals(str2),其中str1和str2都是String对象。 7. 查找子字符串的方法是:str.indexOf(subStr),其中str是一个String对象,subStr是要查找的子字符串。 以下是一个Java程序示例,演示了如何使用String类的一些方法: ```java public class StringDemo { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; String str3 = "Hello"; // 获取字符串长度 System.out.println("Length of str1: " + str1.length()); // 比较字符串是否相等 System.out.println("str1 equals str2? " + str1.equals(str2)); System.out.println("str1 equals str3? " + str1.equals(str3)); // 查找子字符串 System.out.println("Index of 'llo' in str1: " + str1.indexOf("llo")); } } ``` 输出结果为: ``` Length of str1: 5 str1 equals str2? false str1 equals str3? true Index of 'llo' in str1: 2 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值