Java:常用类String

这篇博客详细介绍了Java中的String类,包括其不可变性、常用构造方法如`public String(char[] value)`和`public String(byte[] bytes, int offset, int length)`,以及各种操作方法,如equals、contains、substring、转换和替换功能。还提到了字符串拼接的原理以及如何进行特定操作,如首字母大写、数组转字符串和字符串反转等。" 108932245,5773897,LeetCode 220: 存在重复元素 III 解题思路与代码实现,"['算法', '数据结构', '哈希表', '排序', 'LeetCode']
摘要由CSDN通过智能技术生成

 String

String类是字符串,Java中所有字符串文字都被实现为此类的实例,也就是说,Java中所有带双引号的字符串都是String类的对象

特点:

1.字符串的值不可变,它们在创建后不可被改变;

2.虽然它们的值不可变,但是它们可以被共享;

3.字符串效果上相当于字符数组(char[]),但是它们实际上却是字节数组(byte[]1);

String构造方法

public String()  创建一个空白字符串,不含任何内容;

public String(char[] chs)  根据字符数组,来创建字符串对象

public String(byte[] bys)  根据字节数组,来创建字符串对象

String s="abc";  直接赋值的方式来创建对象,内容就是abc;

public class StringDemo {
    public static void main(String[] args) {
        String s1=new String();
        System.out.println("s1:"+s1);
//        s1:
        char[] chs= {'a','b','c'};
        String s2= new  String(chs);
        System.out.println("s2:"+s2);
//        s2:abc
        byte[] bys={97,98,99};
        String s3=new String(bys);
        System.out.println("s3:"+s3);
//        s3:abc
        String s4="abc";
        System.out.println("s4:"+s4);
//        s4:abc
    }
}

public String() public String(byte[] bytes)

public String(byte[] bytes,int offset,int length)

public String(char[] value)

public String(char[] value,int offset,int count)

public String(String original)

package com.one;

public class StringDemo {
    public static void main(String[] args) {
        //public String()
        String s = new String();
        System.out.println(s); //String类中重写toString()方法
        //查看字符串的长度
//        字符串s的长度为:0
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s.length()); //如果字符串中没有字符,返回0

        System.out.println("=====================================================");
        //public String(byte[] bytes)  //根据一个字节数组创建出一个字符串对象
        byte[] bytes = {97, 98, 99, 100, 101};
        String s2 = new String(bytes);
        System.out.println("s2: " + s2);
        System.out.println("字符串s2的长度为:" + s2.length());
//s2: abcde
//字符串s2的长度为:5
        System.out.println("=====================================================");
        //public String(byte[] bytes,int index,int length)
        //将字节数组中的一部分转化成字符串
        String s3 = new String(bytes, 1, 3);
        System.out.println("s3: " + s3);
        System.out.println("字符串s3的长度为:" + s3.length());
//s3: bcd
//字符串s3的长度为:3
        System.out.println("=====================================================");
        //public String(char[] value)
        //将字符数组转成一个字符串
        char[] c = {'a', 'b', 'c', 'd','我', '爱', '冯', '提', '莫'};
        String s4 = new String(c);
        System.out.println("s4: " + s4);
        System.out.println("字符串s4的长度为:" + s4.length());
//s4: abcd我爱冯提莫
//字符串s4的长度为:9
        System.out.println("=====================================================");
        //public String(char[] value,int index,int length)
        //将字符数组的一部分转成字符串
        String s5 = new String(c, 4, 4);
        System.out.println("s5: " + s5);
        System.out.println("字符串s5的长度为:" + s5.length());
//s4: 我爱冯提莫
//字符串s4的长度为:5
        System.out.println("=====================================================");
        //StringIndexOutOfBoundsException
//       String s6 = new String(c,4,10);
//        System.out.println("s6: "+s6);
//        System.out.println("字符串s5的长度为:" + s6.length());

        System.out.println("=======================================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值