Java 常用类 String类

String 类

String时字符集合类,其底层还是通过封装一个字符数组来实现各种方法
在这里插入图片描述

Stirng三种构造器

public class StringTest {
    public static void main(String[] args) {
        String s1 = new String(); //无参
        String s2 = new String("123"); //字符串参数
        String s3 = new String(new char[]{'1','2','3'}); //字符数组参数
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }
}

在这里插入图片描述其本质都是数组value赋值

常用String方法

  • charAt
public class StringTest {
    public static void main(String[] args) {
        String s2 = new String("123"); //字符串参数
        System.out.println(s2.charAt(2));
    }
}

参数为i,返回下标为i为的字符

  • equals
public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("123");
        String s2 = new String("123"); //字符串参数
        System.out.println(s1.equals(s2));
        System.out.println(s1 == s2);
    }
}

在这里插入图片描述比较,两字符串是否相等,参数为待比较字符串,返回值boolean值
注意,s1 == s2比较的是s1,s2在所以指向的地址值,返回值当然为false

  • compareTo
public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("123");
        String s2 = new String("1234"); 
        System.out.println(s1.compareTo(s2));
    }
}

在这里插入图片描述比较两字符串,若两字符串为包含被包含关系,则输出两字符串长度之差,否则输出第一个字符不相同Ascall码值之差

  • indexOf
public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("1232");
        System.out.println(s1.indexOf('2'));
        System.out.println(s1.indexOf('2',2));
    }
}

在这里插入图片描述

输出给定字符下表,参数为字符和起始地址,其中起始地址可以不写,默认为0,若为找到给定字符则输出-1

  • substring
public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("123abc");
        System.out.println(s1.substring(2));
        System.out.println(s1.substring(3,s1.length()));
    }
}

在这里插入图片描述返回子字符串,参数1为起始地址,参数2为结束地址,范围为左闭右开;参数2可以不写,默认为字符串长度,

  • contact
public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("123abc");
        System.out.println(s1.concat("1234"));
    }
}

在这里插入图片描述在末尾连接字符串

  • replace
public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("123abc");
        System.out.println(s1.replace('1', 'a'));
    }
}

在这里插入图片描述
将字符串中的oldchar替换成newchar,参数1为oldchar,参数2为newchar

  • toLowCase、toUpperCase
public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("ABCabc");
        System.out.println(s1.toLowerCase());
        System.out.println(s1.toUpperCase());
    }
}

在这里插入图片描述
前者将字符串转小写,后者转大写

  • trim
public class StringTest {
    public static void main(String[] args) {
        String s1 = new String(" 1 2 3 ");
        System.out.println(s1.trim());
    }
}

在这里插入图片描述
消去字符串首尾空格

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值