字符串String API深入研究1.0

目录

构造方法详细信息:

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)


点击下方链接学习更多的String类 API深入研究

Sring类的判断功能 API深入研究2.0

String类的获取功能 API深入研究3.0

String类的字符串转换功能 API深入研究4.0

String类的替换功能、去除字符串两端空格、按字典顺序比较两个字符串 API深入研究5.0


在我们企业的日常开发中,public String()是每天都会去使用的高频构造方法,那么我们来深入探索一下。(个人参考API文档编写,仅供学习参考)

字符串:简单理解,就是由多个字符组成的数据,叫做字符串;也可以看作是一个字符数组。

观察API发现:

1、String代表的是字符串,属于java.lang下面的。所以使用的时候不需要导包。

2、String类代表字符串,Java程序中的所有字符串文字(例如"abc"),都被实现为此类的实例(对象)。

3、字符串不变:它们的值在创建后不能被更改,字符串是常量,一旦被赋值,就不能改变。

注意事项:String类重写了toString()方法。


构造方法详细信息:

public String()

查看API文档我们知道:

  初始化新创建的String对象,以使其表示空字符序列。 请注意,使用此构造函数是不必要的,因为Strings是不可变的。

 打开String的原码可以发现String是被final默认修饰的,代表的是常量是不可改变的值。

参考代码1:

public class testStringDemo1 {
    public static void main(String[] args) {
        //public String()
        String s = new String();
        System.out.println("s: " + s);
    }
}

 s: 

Process finished with exit code 0  

 案例:字符串是常量,它的值在创建之后不能更改

String s = “hello”;

s += “world”;

问s的结果是多少?

参考代码:

public class StringDemo2 {
    public static void main(String[] args) {
        String s = "hello";
        s += "world";
        System.out.println(s);

    }
}

 helloworld

Process finished with exit code 0

我们打开原码可以看到: 

​
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

    /** Cache the hash code for the string */
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;

    /**
     * Class String is special cased within the Serialization Stream Protocol.
     *
     * A String instance is written into an ObjectOutputStream according to
     * <a href="{@docRoot}/../platform/serialization/spec/output.html">
     * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
     */
    private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];

    /**
     * Initializes a newly created {@code String} object so that it represents
     * an empty character sequence.  Note that use of this constructor is
     * unnecessary since Strings are immutable.
     */
}

​

 String是被final默认修饰的,所以不能修改。

参考图示:


public String(byte[] bytes)

查看API文档我们知道:

public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException

  构造一个新的String由指定用指定的字节的数组解码charset 。 新的String的长度是字符集的函数,因此可能不等于字节数组的长度。给定字节在给定字符集中无效时,此构造函数的行为是未指定的。 当需要更多的解码过程控制时,应使用CharsetDecoder类。

参数:bytes - 要解码为字符的字节

    charsetName - 支持的名称charset

异常:UnsupportedEncodingException - 如果不支持命名的字符集

参考代码:

public class testStringDemo1 {
    public static void main(String[] args) {
        //public String(byte[] bytes) 将一个字节数组转成一个字符串
        byte[] b = {97,98,99,100,101};
        String s2 = new String(b);
        System.out.println("s2: " + s2);
    }
}

s2: abcde

Process finished with exit code 0


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

查看API文档我们知道:

public int length()

返回此字符串的长度。 长度等于字符串中的数字Unicode code units

Specified by:

length在界面 CharSequence

结果

由该对象表示的字符序列的长度。

参考代码1:

public class testStringDemo1 {
    public static void main(String[] args) {
        //public String()
        String s = new String();
        System.out.println("s: " + s);
        String s1 = new String("qwerdf");
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s.length());
        System.out.println("字符串s的长度为:" + s1.length());
    }
}

 s: 
字符串s的长度为:0
字符串s的长度为:6

Process finished with exit code 0 

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

查看API文档我们知道:

通过使用平台的默认字符集解码指定的字节子阵列来构造新的String 。 新的String的长度是字符集的函数,因此可能不等于子数组的长度。

指定字节在默认字符集中无效时,此构造函数的行为是未指定的。 当需要更多的解码过程控制时,应使用CharsetDecoder类。

参数

bytes - 要解码为字符的字节

offset - 要解码的第一个字节的索引

length - 要解码的字节数

异常

IndexOutOfBoundsException - 如果 offsetlength参数的索引字符在 bytes数组的边界之外

参考代码2:

public class testStringDemo1 {
    public static void main(String[] args) {
        byte[] b = {97,98,99,100,101};
        //public String(byte[] bytes,int index,int length)
        //将字节数组中的一部分截取出来变成一个字符串
        String s3 = new String(b, 1, 3);
        System.out.println("s3: " + s3);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s3.length());
    }
}

s3: bcd
字符串s的长度为:3

Process finished with exit code 0

参考代码3:

public class testStringDemo1 {
    public static void main(String[] args) {
        byte[] b = {97,98,99,100,101};
        //public String(byte[] bytes,int index,int length)
        //将字节数组中的一部分截取出来变成一个字符串
        String s3 = new String(b, 1, 3);
        System.out.println("s3: " + s3);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s3.length());
        System.out.println("***************************************");
        //StringIndexOutOfBoundsException
        String s4 = new String(b, 1, 5);
        System.out.println("s4: " + s4);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s4.length());
    }
}

我们观察s4的运行结果:

 如果截取的lenth长度大于字节数组的长度则会出现报错:

StringIndexOutOfBoundsException 

字符串截取下标越界 


public String(char[] value)

查看API文档我们知道:

  分配一个新的String ,以便它表示当前包含在字符数组参数中的字符序列。 字符数组的内容被复制; 字符数组的后续修改不会影响新创建的字符串。

参数

value - 字符串的初始值

 顾名思义就是将一个字符数组转换成一个字符串。

参考代码:

public class testStringDemo1 {
    public static void main(String[] args) {
        //public String(char[] value) 将一个字符数组转成一个字符串
        char[] c = {'a','b','c','d','我','爱','写','代','码'};
        String s5 = new String(c);
        System.out.println("s5: " + s5);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s5.length());
    }
}

 s5: abcd我爱写代码
字符串s的长度为:9

Process finished with exit code 0


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

查看API文档我们知道:

分配一个新的String ,其中包含字符数组参数的子阵列中的字符。 offset参数是子阵列的第一个字符的索引, count参数指定子阵列的长度。 副本的内容被复制; 字符数组的后续修改不会影响新创建的字符串。

参数

value - 作为字符源的数组

offset - 初始偏移量

count - 长度

异常

IndexOutOfBoundsException - 如果 offsetcount参数的索引字符超出了 value数组的界限

 顾名思义就是将字符数组中的一部分截取出来,变成一个字符串。

参考代码1:

public class testStringDemo1 {
    public static void main(String[] args) {
        //public String(char[] value) 将一个字符数组转成一个字符串
        char[] c = {'a','b','c','d','我','爱','写','代','码'};
        String s5 = new String(c);
        System.out.println("s5: " + s5);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s5.length());
        System.out.println("***************************************");
        //public String(char[] value,int offset,int count)
        //将字符数组中一部分截取出来变成一个字符串
        String s6 = new String(c, 4, 5);
        System.out.println("s6: " + s6);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s6.length());
    }
}

s5: abcd我爱写代码
字符串s的长度为:9
***************************************
s6: 我爱写代码
字符串s的长度为:5

Process finished with exit code 0

 观察API文档我们得知如果count的长度字符数组的长度则会出现异常。

参考代码2:

public class testStringDemo1 {
    public static void main(String[] args) {
        //public String(char[] value) 将一个字符数组转成一个字符串
        char[] c = {'a','b','c','d','我','爱','写','代','码'};
        String s5 = new String(c);
        System.out.println("s5: " + s5);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s5.length());
        System.out.println("***************************************");
        //public String(char[] value,int offset,int count)
        //将字符数组中一部分截取出来变成一个字符串
        String s6 = new String(c, 4, 5);
        System.out.println("s6: " + s6);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s6.length());
        System.out.println("***************************************");
        String s7 = new String(c,4,6);
        System.out.println("s7:" + s7);
    }
}

我们观察S7的运行结果: 

 当字符串count截取长度大于字符数组的长度则会报错:

StringIndexOutOfBoundsException

字符串截取下标越界


public String(String original)

查看API文档我们知道:

初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。 除非需要original的显式副本, original使用此构造函数是不必要的,因为Strings是不可变的。

参数

original - A String

参考代码:

    public static void main(String[] args) {
        //public String(String original)
        String s7 = "你好";
        String s8 = new String(s7);
        System.out.println("s8: " + s8);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s8.length());
    }
}

 s8: 你好
字符串s的长度为:2

Process finished with exit code 0

参考图示:


 到底啦!你有学到什么吗?来给靓仔一个关注吧!

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

liangzai2048

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

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

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

打赏作者

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

抵扣说明:

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

余额充值