建议收藏,花了一个星期整理的String类的方法总结,讲的非常详细

一、Constructor Summary/构造函数的总结

① public String()

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

创建字符串对象最常用的一种方式。

String s1 = "hello world!";
② public String(byte[] bytes)

通过使用平台的默认字符集解码指定的字节数组构造了一个新的 String。

​​​把byte数组全部转为字符串。

byte[] bytes = {97, 98, 99};//在ASCII码中97/a 98/b 99/c
String s2 = new String(bytes);
System.out.println(s2);//abc
③ public String(byte[] bytes, int offset, int length)

通过使用平台的默认字符集解码指定的字节子阵列来构造新的String 。
把byte数组中的一部分转为字符串
byte[] bytes:字节数组
int offset:元素下标的起始位置 
int length:长度

byte[] bytes = {97, 98, 99};//在ASCII码中97/a 98/b 99/c
String s3 = new String(bytes,1,2);
System.out.println(s3);//bc
④ public String(char[] value)

将char数组全部转为字符串

char[] chars = {'我','是','友','人','A'};
String s4 = new String(chars);
System.out.println(s4);//我是友人A
⑤ public String(char[] value, int offset, int count)

将char数组部分转为字符串

String s6 = new String("哈哈哈");
⑥ public String(Strin
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值