您的位置 首页 java

初识java—(三十四)String、StringBuffer和StringBuilder类

7.3.2 string StringBuffer StringBuilder

7.3.2.1String内容补充:

(1) 字符串 是常量,它的值在创建之后不能更改

String str = “hello”; str+=”world”;问str的结果是多少?

初识java—(三十四)String、StringBuffer和StringBuilder类

(2) String str = new String(“hello”) 和 string str1 = “hello”;的区别

前者会创建2个对象,后者创建一个对象。

初识java—(三十四)String、StringBuffer和StringBuilder类

(3)==与equals

==:比较引用类型的是地址值是否相同

equals:比较引用类型默认也是比较地址值是否相同,而 String类 重写了equals()方法,比较的是内容是否相同。

(4)看程序写结果

 String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

String s3 = new String("hello");。
String s4 = "hello";
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));

String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);
System.out.println(s5.equals(s6));  

(5)看程序写结果

 String s1 = "hello";
String s2 = "world";
 String  s3 = "helloworld";
System.out.println(s3 == s1 + s2);
System.out.println(s3.equals((s1 + s2)));
System.out.println(s3 == "hello" + "world");
System.out.println(s3.equals("hello" + "world"));  

结论:

字符串如果是变量相加,先开空间,再拼接。

字符串如果是常量相加,先加,再在常量池找,如果有直接返回,否则,则创建一个新的。

这里依旧可以用一下反编译软件来看一下。

7.3.2.2正式内容开始:

字符串就是一连串的字符序列, Java 提供了String和StringBuffer两个类来封装字符串,并提供了一系列方法来操作字符串对象。

String类是不可变类,即一旦一个String对象被创建后,包含在这个对象中的字符序列是不可改变的,直至这个对象的销毁。

StringBuffer对象则代表一个字符序列可变的字符串,当一个StringBuffer被创建以后,通过StringBuffer提供的 append ()、insert()、reverse()、setCharAt()、setLength()等方法可以改变这个字符串对象的字符序列。一旦通过StringBuffer生成了最终想要的字符串,就可以调用它的 toString ()方法将其转换为一个String对象。通过StringBuffer对象,我们在拼接字符串的时候,就不需要每次都要创建一个新的对象了,而是直接可以通过转换来对字符串做一个拼接操作。

StringBuilder类,也代表了字符串对象。StringBuilder和StringBuffer基本相似,两个类的 构造器 和方法也基本相同。不同的是StringBuffer是线程安全的,StringBuilder没有实现线程安全。因此在通常情况下优先考虑使用StringBuffer。

String类的构造器:

String():创建一个包含0个字符串序列的String对象。

String( byte [] bytes,Charset charset):使用指定的 字符集 将指定的byte[]数组解码成一个新的String对象。

String(byte[] bytes,int offset,int length):使用平台的默认字符集将从指定的byte[]数组的offset开始,长度为length的子数组成一个新的String对象。

String(byte[] bytes,int offset,int length,String charsetName):使用指定的字符集将指定的byte[]数组从offset开始,长度为length的子数组解码成一个新的String对象。

String(byte[] bytes,String charsetName):使用指定的字符集将指定的byte[]数组解码成一个新的String对象。

String(char[] value,int offset,int count):将指定的 字符数组 从offset开始,长度为count的字符元素连接成一个字符串。

String(String original):根据字符串常量来创建一个String对象。也就是说新创建的String对象是该参数字符串的副本。

String类提供大量方法来操作字符串对象:

char charAt(int index):获取字符串中指定位置的字符。

int compareTo(String anotherString):比较两个字符串大小,如果两个字符串的字符序列相等,则返回0,不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差。如果较长的字符串的前面部分与较短的字符串一样,则返回他们的长度差。

举例1:

 String s1 = “abcdef”;
String s2 = “abcdefjhi”;
String s3 = “abcdefk”;
System.out.println(s2.compareTo(s1)); //返回3
System.out.println(s2.compareTo(s3)); //返回 -1  

String concat(String str):将该String对象与str连接在一起。与Java提供的字符串连接运算符“+”相同。

boolean contentEquals(StringBuffer sb):将该String对象与StringBuffer对象sb进行比较,当他们包含的字符序列相同时返回true。

static String copy ValueOf(char[] data):将字符数组组成一个字符串,与构造器String(char[] content)功能相同。

static String copyValueOf(char[] data,int offset,int count):将char数组的子数组中的元素连缀成字符串,与String(char[] value,int offset,int count)构造器功能相同。

boolean endsWith(String str):返回String对象是否以str字符串结尾。

boolean equals(Object anObject):将该字符串与指定对象进行比较,如果二者包含序列相等则返回true,否则返回false。

boolean equalsIgnoreCase(String str):将字符串与指定的对象进行比较,二者包含序列相同则返回true,否则返回false。只是该方法忽略大小写。

 byte[] getBytes():将该String对象转换成byte数组。
String str = "abcd";
byte[] data = str.getBytes();
System.out.println(data.length); //4
System.out.println((char)data[0]); //a  

void get char s(int srcBegin,int srcEnd,char[] dst,int dstBegin):该方法将字符串中从srcBegin开始到srcEnd结束的字符复制到dst字符数组中,其中dstBegin为目标字符数组的要拷贝的起始位置。

 public static void main(String[] args) throws  Exception {
char[] c1 = {'我','爱','北','京'};
String str = "济南";
str.getChars(0, 2, c1, 2);
System.out.println(c1);
}  

int indexOf(int ch):找出ch字符在该字符串中第一次出现的索引位置。

int indexOf(int ch,int fromIndex):找出ch字符在该字符串中从fromIndex索引后面第一次出现的位置。

 public static void main(String[] args) throws Exception{
String str = "你好,我好,他也好";
System.out.println(str.indexOf('好'));
System.out.println(str.indexOf('好', 3));
}  

int indexOf(String str):找出str子字符串在该字符中第一次出现的位置。

int indexOf(String str,int fromIndex):找出str子字符串在该字符串中从fromIndex索引后第一次出现的位置。

 public static void main(String[] args) throws Exception{
String str = "中华民族是一个56个民族的总称!";
System.out.println(str.indexOf("民族"));
System.out.println(str.indexOf("民族", 3));
}  

int lastIndexOf(int ch):找出ch字符在该字符串中最后一次出现的位置。

int lastIndexOf(int ch,int fromIndex):找出ch字符在该字符串中从fromIndex开始最后一次出现的位置。

 public static void main(String[] args) throws Exception{
String str = "中华民族是一个56个民族的总称!";
System.out.println(str.lastIndexOf('民'));
System.out.println(str.lastIndexOf('民', 3));
}  

int lastIndexOf(String str):找出str字符串在该字符串中最后一次出现的位置。

int lastIndexOf(String str,int formIndex):找出str字符串在该字符串中从fromIndex索引开始后最后一次出现的位置。

int length():返回当前字符串的长度。

 public static void main(String[] args) throws Exception{
String str = "中华民族是一个56个民族的总称!";
int length = str.length();
System.out.println(length);
}  

String replace(String oldChar,String newChar):将字符串中的所有的oldChar替换成newChar。

String replaceAll(String regex, String replacement):使用给定的 replacement 替换此字符串所有匹配给定的正则表达式regex的子字符串。

 public static void main(String[] args) throws Exception{
String msg = "你好,我也好,大家好才是真的好";
String oldChar = "你好";
String newChar = "伱好";
msg = msg.replace(oldChar, newChar);
System.out.println(msg);
oldChar = "好";
newChar = "坏";
msg = msg.replaceAll(oldChar, newChar);
System.out.println(msg);
}  

char[] toCharArray():将该字符串对象转换成字符数组。

String toLowerCase():将字符串转换成小写。

String toUpperCase():将字符串转换成大写。

String[] split(String reg):将字符串按照指定的正则表达式进行拆分。

String subString(int index):从指定的索引的位置开始直到字符串结束位置,将其作为字符串进行返回。

String subString(int beginIndex,int endIndex)

StringBuffer字符串缓冲区:

基本的方法看API。

主要看下面这个问题,String和StringBuffer作为形参传递的问题。

 public static void main(String[] args) {
String str1 = "hello";
String str2 = "world";
System.out.println(str1 + "--" + str2);
change(str1, str2);
System.out.println(str1 + "--" + str2);

StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
System.out.println(sb1 + "--" + sb2);
change(sb1, sb2);
System.out.println(sb1 + "--" + sb2);

String s1 = "a";
String s2 = "b";
String s3 = "ab";
System.out.println(s3 == "a" + "b");
System.out.println(s3 == (s1 + s2));
}

public static void change(StringBuffer sb1, StringBuffer sb2) {
sb1 = sb2;
sb2.append(sb1);
}

public static void change(String str1, String str2) {
str1 = str2;
str2 = str1 + str2;
}  

字符串是一种特殊的引用类型,我们可以把它当作是常量值来看,它的形参改变,对本身没有任何的影响,和基本类型差不多,看成常量值就好了。

记住这一句话,String作为参数传递,效果和基本类型作为参数传递是一样的。

文章来源:智云一二三科技

文章标题:初识java—(三十四)String、StringBuffer和StringBuilder类

文章地址:https://www.zhihuclub.com/189856.shtml

关于作者: 智云科技

热门文章

网站地图