String class, StringBuffer class

 

String class:

The String class represents character strings. The value of the string cannot be changed after it is created. The string itself cannot be changed, but the address value recorded in the str variable can be changed. There are a large number of overloaded construction methods in the String class.


String s1 = new String(); //创建String对象,字符串中没有内容
	
byte[] bys = new byte[]{97,98,99,100};
String s2 = new String(bys); // 创建String对象,把数组元素作为字符串的内容
String s3 = new String(bys, 1, 3); //创建String对象,把一部分数组元素作为字符串的内容,参数offset为数组元素的起始索引位置,参数length为要几个元素
	
char[] chs = new char[]{’a’,’b’,’c’,’d’,’e’};
String s4 = new String(chs); //创建String对象,把数组元素作为字符串的内容
String s5 = new String(chs, 0, 3);//创建String对象,把一部分数组元素作为字符串的内容,参数o                   ffset为数组元素的起始索引位置,参数count为要几个元素
 
String s6 = new String(“abc”); //创建String对象,字符串内容为abc
 

StringBuffer class:

StringBuffer is a string buffer, it is a container, can hold many strings, and can operate.

创建一个字符串缓冲区对象。用于存储数据。
StringBuffer sb = new StringBuffer();
sb.append("haha"); //添加字符串
sb.insert(2, "it");//在指定位置插入
sb.delete(1, 4);//删除
sb.replace(1, 4, "cast");//替换指定范围内的内容
String str = sb.toString();

 

Guess you like

Origin blog.csdn.net/qq_34159161/article/details/106869230