【Java】String类

  1.字符串的构造

        String提供的构造方式有很多,但常用的就三种:

//1
String str1 = "hello";
//2
String str2 = new String("hello");
//3
char [] arr = {'h','e','l','l','e'};
String str3 = new String(arr);

注意:

1. String是一个引用类型,内部储存的不是字符串的本身;

2.在Java中,""引起来也是String对象;

2.String对象的比较

在Java中字符串的比较提供了四种。

2.1 ==

public class Test1 {
    public static void main(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
        String str3 = "haha";
        String str4 = str1;
        System.out.println(str1 == str2);//false
        System.out.println(str1 == str4);//true
        System.out.println(str1 == str3);//false
    }
}

        看到上面代码,有人就会有疑惑,为什么str1 == str2是false呢?他们两个不是一样的吗?其实是不一样的,两个new分别new出不同对象,虽然对象中内容相同,但对于引用类型变量, ==比较的是两个引用变量引用的是否为同一个对象。

2.2 equals

public class Test1 {
    public static void main(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
        String str3 = new String("Hello");
        System.out.println(str1.equals(str2));//true
        System.out.println(str1.equals(str3));//false
    }
}

        本来的equals方法比较规则和 == 相同,但是String类中重写了equals方法,使其比较方式变成比较两个字符串内容是否相同。返回值类型是Boolean。

2.3 compareTo

public class Test1 {
    public static void main(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
        String str3 = new String("Hello");
        System.out.println(str1.compareTo(str2));//0
        System.out.println(str1.compareTo(str3));//32
    }
}

        compareTo方法返回值类型是int型,两个字符串逐个比较,当碰到不等的字符,直接返回两个字符差。

2.4 compareToIgnoreCase

public class Test1 {
    public static void main(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
        String str3 = new String("Hello");
        System.out.println(str1.compareTo(str2));//0
        System.out.println(str1.compareToIgnoreCase(str3));//0
    }
}

        compareToIgnoreCase方法在比较的时候忽略大小写,其他的和compareTo方法相同。

3.字符串的查找

方法功能
charAt(int index)返回index位置上的字符,index为负数或越界,抛出异常
 indexOf(char ch)返回ch第一次的位置,没有的返回-1
indexOf(char ch,int fromIndex)

返回从fromIndex开始第一次出现ch的位置,

没有返回-1

indexOf(String str)返回str第一次出现的位置,没有返回-1
indexOf(String str,int fromIndex)返回从fromIndex开始第一次出现str的位置,没有返回-1
lastIndex(char ch)倒的找,第一次出现ch的位置,没有-1
lastIndex(char ch,int fromIndex)倒着从fromIndex位置找第一次出现ch的值,没有返回-1
lastIndex(String str)倒着找,第一次出现str的值,没有返回-1
lastIndex(String str,int formIndex)倒着从formIndex位置找,第一次出现str的值,没有返回-1
public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); // 'b'
System.out.println(s.indexOf('c')); // 6
System.out.println(s.indexOf('c', 10)); // 15
System.out.println(s.indexOf("bbb")); // 3
System.out.println(s.indexOf("bbb", 10)); // 12
System.out.println(s.lastIndexOf('c')); // 17
System.out.println(s.lastIndexOf('c', 10)); // 8
System.out.println(s.lastIndexOf("bbb")); // 12
System.out.println(s.lastIndexOf("bbb", 10)); // 3

4.字符串的转化

4.1 数字和字符串之间的转化

public class Test1 {
    public static void main(String[] args) {
        //数字转字符串
        String s1 = String.valueOf(132);
        String s2 = String.valueOf(true);
        String s3 = String.valueOf(12.33);
        System.out.println(s1);//132
        System.out.println(s2);//true
        System.out.println(s3);//12.33
        //字符串转数字
        int i = Integer.parseint("132");
        double d = Double.parsedouble("12.33");
        System.out.println(i);//132
        System.out.println(d);//12.33
    }
}

为什么不使用new直接创建String对象,而是调用valueOf多此一举呢?

1. 这是因为当显式new  String对象时,里面对象为null,那么就会发生错误,而调用valueOf并不会。

        

    public static void main(String[] args) {
         Object o = someMethodThatMayReturnNull();
//            String string = new String(d); 报错
        String s = String.valueOf(o);
    }

    private static Object someMethodThatMayReturnNull() {
        return null;
    }

2. 调用valueOf也可用于连接字符串,将多个值拼接成一个字符串。

    public static void main(String[] args) {
         int d = 1;
         double e = 1.2;
        //    String string = new String(d); 报错
         String string = String.valueOf(d + e);
        System.out.println(string);
    }

4.2 大小写的转化

public class Test1 {
    public static void main(String[] args) {
        String s1 = "HELLO";
        String s2 = "haha";
        System.out.println(s1.toLowerCase());//转化小写
        System.out.println(s2.toUpperCase());//转化大写
    }
}

4.3 字符串与数组之间的转换

public class Test1 {
    public static void main(String[] args) {
        char [] arr1 = {'h','e','l','l','o'};
        String s1 = "haha";
        String s2 = new String(arr1);//数组转化字符串
        char [] arr2 = s1.toCharArray();//字符串转数组
        System.out.println(Arrays.toString(arr2));
        System.out.println(s2);
    }
}

5.字符串的替换

public class Test1 {
    public static void main(String[] args) {
        String s = "asddss";
        System.out.println(s.replace('s','d'));//将一种字符全部替换成新的字符
        System.out.println(s.replaceAll("s","d"));//将一种字符串全部替换成新的字符串
        System.out.println(s.replaceFirst("s","d"));//将一种第一次字符串出现替换成新的字符串

    }
}

6.字符串的拆分


public class Test1 {
    public static void main(String[] args) {
        String s = "hellojavaworld";
        String [] strings1 = s.split("a");//将s按a拆分
        System.out.println(Arrays.toString(strings1));
        String [] strings2 = s.split("a",2);//将s按a拆分,但是分成两组
        System.out.println(Arrays.toString(strings2));
        
    }
}
//运行结果
//[helloj, v, world]
//[helloj, vaworld]

注意:

        在拆分一些特殊的字符,要进行转义。

        例如:

  • 字符"|","*","+"都得加上转义字符,前面加上 "\\" ;
  • 而如果是 "\" ,那么就得写成 "\\\\" ;
  •  如果一个字符串中有多个分隔符,可以用"|"作为连字符;

7.字符串的截断

public class Test1 {
    public static void main(String[] args) {
        String s = "helloworld";
        String s1 = s.substring(1);//从坐标1的位置开始截断
        String s2 = s.substring(1,5);//从坐标1的位置开始截断,截到5坐标,注意不包括5坐标
                                     //在Java中这种都是前闭后开的
        System.out.println(s1);
        System.out.println(s2);
    }
}
//运行结果:
//elloworld
//ello

8.去除字符串左右空格

public class Test1 {
    public static void main(String[] args) {
        String s = "   as s  s     ";
        System.out.println(s);
        System.out.println(s.trim());//去除左右的空格
    }
}
//运行结果:
//   as s  s     
//as s  s

注意:

        字符串的修改,都是创建一个新对象,而不是在原来的字符串上修改。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是小辰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值