String类
概述
java.lang.Stringjava.lang.String
类代表字符串。Java程序中所有的字符串文字(例如"abc"
)都可以被看作是实现此类的实例。
类 String
中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻译为大写或小写的所有字符的字符串的副本。
特点
- 字符串不变:字符串的值在创建后不能被更改。
String s1 = "abc";
s1 += "d";
System.out.println(s1); // "abcd"
// 内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。
- String对象是不可变的,所以它们可以被共享。
String s1 = "abc";
String s2 = "abc";
// 内存中只有一个"abc"对象被创建,同时被s1和s2共享。
- "abc"
等效于
char[] data={ ‘a’ , ‘b’ , ‘c’ }`。
String str = "abc";
等同于:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
// String底层是靠字符数组实现的。
在 Java 9 之后,String 类的实现改用 byte 数组存储字符串 private final byte[] value;
构造方法
java.lang.String
:此类不需要导入
public String()
:初始化新创建的 String对象,以使其表示空字符序列。public String(char[] value)
:通过当前参数中的字符数组来构造新的String。public String(byte[] bytes)
:通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
// 无参构造
String str = new String();
// 通过字符数组构造
char chars[] = {'a', 'b', 'c'};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = { 4, 44, 444 };
String str3 = new String(bytes);
方法介绍
判断功能方法
public boolean equals (Object anObject)
:将此字符串与指定对象进行比较。public boolean equalsIgnoreCase (String anotherString)
:将此字符串与指定对象进行比较,忽略大小写。
public class String_Demo01 {
public static void main(String[] args) {
// 创建字符串对象
String s1 = "four";
String s2 = "four";
String s3 = "FOUR";
// boolean equals(Object obj):比较字符串的内容是否相同
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
System.out.println("-----------");
//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // true
System.out.println("-----------");
}
}
获取功能方法
public int length ()
:返回此字符串的长度。public String concat (String str)
:将指定的字符串连接到该字符串的末尾。public char charAt (int index)
:返回指定索引处的 char值。public int indexOf (String str)
:返回指定子字符串第一次出现在该字符串内的索引。public String substring (int beginIndex)
:返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。public String substring (int beginIndex, int endIndex)
:返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
public class String_Demo02 {
public static void main(String[] args) {
//创建字符串对象
String s = "helloworld";
// int length():获取字符串的长度,其实也就是字符个数
System.out.println(s.length());
System.out.println("--------");
// String concat (String str):将将指定的字符串连接到该字符串的末尾.
String s = "helloworld";
String s2 = s.concat("**hello JAVA");
System.out.println(s2);// helloworld**hello JAVA
// char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("--------");
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回-1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("J"));
System.out.println(s.indexOf("aka"));
System.out.println("--------");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("--------");
// String substring(int start,int end):从start到end截取字符串。含start,不含end。
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}
转换功能方法
public char[] toCharArray ()
:将此字符串转换为新的字符数组。public byte[] getBytes ()
:使用平台的默认字符集将该 String编码转换为新的字节数组。public String replace (CharSequence target, CharSequence replacement)
:将与target匹配的字符串使用replacement字符串替换。
CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。
public class String_Demo03 {
public static void main(String[] args) {
//创建字符串对象
String s = "abcde";
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("-----------");
// byte[] getBytes ():把字符串转换为字节数组
byte[] bytes = s.getBytes();
for(int x = 0; x < bytes.length; x++) {
System.out.println(bytes[x]);
}
System.out.println("-----------");
// 替换字母it为大写IT
String str = "itcast itheima";
String replace = str.replace("ab", "AB");
System.out.println(replace); // abcde ABcde
System.out.println("-----------");
}
}
分割功能方法
public String[] split(String regex)
:将此字符串按照给定的regex(规则)拆分为字符串数组。
public class String_Demo03 {
public static void main(String[] args) {
//创建字符串对象
String s = "aa|bb|cc";
String[] strArray = s.split("|"); // ["aa","bb","cc"]
for(int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]); // aa bb cc
}
}
}
因为在String类的特点里提过字符串的值在创建后不能被更改,对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,所以需要使用到StringBuilder类
StringBuilder类
java.lang.StringBuilder
查阅其API可知StringBuilder又名可变字符序列,是一个类似于 String 的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容
构造方法
- public StringBuilder() :构造一个空的StringBuilder容器
- public StringBuilder(String str) :构造一个StringBuilder容器,并将字符串添加进去
基本常用方法
- public StringBuilder append(…) :添加任意类型数据的字符串形式,并返回当前对象自身。
- public String toString() :将当前StringBuilder对象转换为String对象。
public static void main(String[] args) {
/* //无参构造
StringBuilder stringBuilder = new StringBuilder();
System.out.println(stringBuilder);//空白*/
//有参构造
StringBuilder sb = new StringBuilder("StringBuilder");
// System.out.println(sb);//StringBuilder
//append方法
StringBuilder sbAppend = sb.append(1).append("StringBuilder").append(System.currentTimeMillis());
System.out.println(sb==sbAppend);//true
//StringBuilder已经覆盖重写了Object当中的toString方法。
System.out.println(sb);//StringBuilder1StringBuilder1587950808311
//toString方法
String s = sb.toString();
System.out.println(s);//StringBuilder1StringBuilder1587950808311
}