JAVA-SE-String类

一,构造方法

我们在这里介绍三个常用的构造方法:

public class Test {
    public static void main(String[] args) {
        // 第一种 -》直接new一个
        String str = new String("abcdef");

        // 第二种 -》实际上是第一种的精简版
        String str1 = "abcdef";

        // 第三种 -》可以输入一个字符数组,并将其转换成字符串
        char[] ch = {'a','b','c'};
        String str2 = new String(ch);
    }
}

二,String类对象的比较

2.1 equals方法 - - - 比较是否相等

返回值是 boolean 类型,要想看详细的equals方法介绍,可以看我写的 Object类 的那篇博客

public class Test {
    public static void main(String[] args) {
        String str1 = "1234";
        String str2 = "abc";
        boolean ans = str1.equals(str2);
        System.out.println(ans);
    }
}

2.2  == - - - 只能比较地址 

public class Test {
    public static void main(String[] args) {
        String str1 = new String("1234");
        String str2 = new String("1234");
        System.out.println(str2 == str1);
    }
}

2.3 compareTo方法 - - - 比较大小

返回值是 int 类型,String类可以直接使用compareTo方法,因为JAVA在String类中已经重写了该方法。

比较方式:1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值

返回值 > 0 : str1 > str2   ;返回值 = 0 : str1 = str2  ;返回值 < 0 : str1 < str2

public class Test {
    public static void main(String[] args) {
        String str1 = new String("1234");
        String str2 = new String("1234");
        int ans = str1.compareTo(str2);
        System.out.println(ans);
    }
}

2.4 compareToIgnoreCase方法

与compareTo方法一样,只不过它的比较不区分大小写,比如 'A' 和 'a' 是一样的

三, 字符串查找

3.1 charAt()方法

在JAVA中字符串不能像C语言中的字符串一样用 str[i] 去访问,所以JAVA中我们使用charAt方法,()中填下表,从0开始

public class Test {
    public static void main(String[] args) {
        String str = "abcdefg";
        char ch = str.charAt(1);
        System.out.println(ch);
    }
}

3.2  indexOf()方法

方法:int indexOf(int ch) 

作用:返回ch第一次出现的位置,没有返回-1

public class Test {
    public static void main(String[] args) {
        String str = "abcdefg";
        int ret = str.indexOf('b');
        System.out.println(ret);
    }
}

 

3.3 其他方法

方法用法
int indexOf(int ch, int
fromIndex)
从fromIndex位置开始找ch第一次出现的位置,没有返回-1
int indexOf(String str)返回str第一次出现的位置,没有返回-1
int indexOf(String str, int
fromIndex)
从fromIndex位置开始找str第一次出现的位置,没有返回-1
int lastIndexOf(int ch)从后往前找,返回ch第一次出现的位置,没有返回-1
int lastIndexOf(int ch, int
fromIndex)
从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1
int lastIndexOf(String str)从后往前找,返回str第一次出现的位置,没有返回-1
int lastIndexOf(String str, int
fromIndex)
从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1

四,转换

4.1 数值和字符串转化

 注意 :Boolean类型也可以转化!

 也可以将字符串转换成数值

4.2 大小写转化

toLowerCase()转小写,toUpperCase()转大写

 

 4.3 字符串转数组 

public class Test {
    public static void main(String[] args) {
        String s = "hello";
        // 字符串转数组
        char[] ch = s.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶祇秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值