String类

  String类的路径java.lang.String

创建字符串

String str = "Hello World";

String类下的方法

  • toString()
    返回字符串本身,返回值为 String 类型。

  • concat()
    将两个字符串连接在一起

        public static void main(String[] args) {
            String str1 = "Hello";
            String str2 = " World!";
            String concatstr = str1.concat(str2);
            System.out.println(concatstr); // Hello World!
        }
    
  • equals()
    比较两个字符串的值

        public static void main(String[] args) {
            String str1 = "Hello";
            String str2 = "Hello";
            Boolean result = str1.equals(str2);
            System.out.println(result); // true
        }
    
  • getBytes()
    将字符串转换为字节数组

        public static void main(String[] args) {
            String str1 = "Hello";
            byte[] str1_byte = str1.getBytes();
            System.out.println(Arrays.toString(str1_byte)); // [72, 101, 108, 108, 111]
        } 
    
  • indexOf()
    返回字符串中指定字符或字符串的位置,如果没有找到指定的字符或者字符串,返回-1。

        public static void main(String[] args) {
            String str1 = "Hello";
            int ret1 = str1.indexOf("H");
            int ret2 = str1.indexOf("e");
            int ret3 = str1.indexOf("a");
            System.out.println("str1: " + ret1); // 0
            System.out.println("str2: " + ret2); // 1
            System.out.println("str3: " + ret3); // -1
        }
    
  • length()
    返回字符串字符序列的长度(包含空格)。

    public static void main(String[] args) {
        String str1 = "Hello ";
        int length = str1.length();
        System.out.println(length); // 6
    }
  • isEmpty()
    判断字符串是否为空,返回值为 boolean 类型(根据length来判断)。
    public static void main(String[] args) {
        String str1 = "Hello ";
        Boolean isempty = str1.isEmpty();
        System.out.println(isempty); // false
    }
  • substring(…)
    这个方法有两个重载方法substring(int beginIndex)substring(int beginIndex, int endIndex)
    substring(int beginIndex)返回从 beginIndex 开始到原字符串末尾的子字符串。
    substring(int beginIndex, int endIndex)返回从 beginIndex 开始到 endIndex 结束(不包括 endIndex)的子字符串。
    substring() 方法不会修改原始字符串,它返回的是一个新的字符串对象。
        public static void main(String[] args) {
            String str1 = "Hello World!";
            String sub1 =  str1.substring(6);
            System.out.println(sub1); // World!
            System.out.println(str1.length()); // 12
            String sub2 =  str1.substring(6,12);
            System.out.println(sub2); // World!
        }
    
  • replace()
    public String replace(CharSequence target, CharSequence replacement)把旧的字符或字符序列替换为新的字符或字符序列。
        public static void main(String[] args) {
            String str1 = "Hello World!";
            String str2 = "Hello World!".replace("Hello","NiHao");
            System.out.println(str2); // NiHao World!
        }
    
  • String.valueOf()
    将其他数据类型的数据转换为字符串类型
        public static void main(String[] args) {
            int num = 100;
            String strnum = String.valueOf(num);
            System.out.println(strnum); # 100
        }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值