有关String类的常用知识

1、final,不可以有子类

2、String str = new String(“hello”);

     String str2 = “hello”;

3、字符串不可变

      final char[] value;

4、String   长度不可变

     StringBuilder,StringBuffer是可变的字符串

     StringBuilder  是线程安全的

     StringBuffer 是线程安全的

     char[]  value;

5、String类的常用方法

      str.length();  //数组使用属性,字符串使用方法

      str.trim();     //去掉字符串的前后空格

      str.equalsIgnoreCase("ABC");   //不区分大小写

      str.charAt(2);  //

6、String的构造方法

      方法一:对于类类型,直接使用toString()方法来构造                


Integer number=3;
String numStr=number.toString();

    方法二:对于基本数据类型,可以使用对应的类的静态的toString()方法来构造

int a=3;
byte b=25;
String aStr=Integer.toString(a);
String bStr=Byte.toString(b);

    方法三:使用new String(char[] value),用字符数组构造一个String。

char[] chars={'h','e','l'.'l','o'};
String str=new String(chars);

方法四:直接给一个String变量赋值或者使用new String(String value)来构造。当都是直接赋值的方式时,两个字符串用==比较的值为true,当一个是直接赋值,一个用new String,用==比较时为false。

String a="hello";
String b="hello";
String c=new String("hello");

方法五:使用StringBuffer或StringBuilder构造一个String。String是常量类型,如果需要有大量的字符串操作,使用String的执行速度会慢很多。对于有大量字符串操作的,通过使用StringBuffer或StringBuilder完成字符串操作后,再转换成String。

StringBuffer buffer=new StringBuffer("hello");
String buffer1=new String(buffer);

StringBuilder builder=new StringBuilder("hello");
String builder2=new String("hello");

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值