Java中用于处理字符串常量的有三个类:
java.lang.Stirng
java.lang.StringBuilder
java.lang.lang.StringBuilder
本文先介绍String类。
String
类表示字符串。Java程序中的所有字符串文字(例如"abc"
)都实现为此类的实例。
字符串是不变的; 它们的值在创建后无法更改。 字符串缓冲区支持可变字符串。 因为String对象是不可变的,所以可以共享它们(如果两个字符串的内容相同,则采用同一块内存地址)。
举例:
public class Test01 {
public static void main(String[] args) {
String text1 = "123465";
String text2 = "123465";
System.out.println(text1 == text2);
}
}
复制代码
结果为:true
==比较的是内存地址。
如果是new出来的 ,那一定是不同的内存地址。
举例:
public class Test01 {
public static void main(String[] args) {
String text1 = "123465";
String text2 = new String("123456");
System.out.println(text1 == text2);
}
}
复制代码
结果为:false
1. charAt(int index)
返回指定索引处的 char
值。
public class Test01 {
public static void main(String[] args) {
String text1 = "123465";
System.out.println(text1.charAt(2));
}
}
复制代码
结果为:3
2. codePointAt(int index)
返回指定索引处的字符(Unicode代码点)。
public class Test01 {
public static void main(String[] args) {
String text1 = "abcdefg";
System.out.println(text1.codePointAt(2));
}
}
复制代码
结果为:99
3. compareTo(String anotherString)
按字典顺序比较两个字符串。
public class Test01 {
public static void main(String[] args) {
String text1 = "abcdefg";
String text2 = "1234567";
System.out.println(text1.compareTo(text2));
}
}
复制代码
4. compareToIgnoreCase(String str)
简单来说,String类的compareTo()方法是用来比较两个字符串的字典顺序。 用字符串1跟字符串2作比较,如果字符串1的字典顺序在字符串2前面,则返回一个负数。若在后面,则返回一个正数。若两个字符串的字典顺序相同,则返回0。 这里的字典顺序指的是ASCII码表中的字符顺序。ASCII表中每个字符都有对应的下标,从0开始升序排列,共128个字符。
public class Test01 {
public static void main(String[] args) {
String text1 = "abcdefg";
String text2 = "AaBbCcdD";
//字符串1的第一个字符跟字符串2的第一个字符不相等,
// 则两个字符串都按照第一个字符的ASCII码顺序进行比较,
// 其他字符都不用看,并返回一个整型。
System.out.println(text1.compareTo(text2));
}
}
复制代码
结果为:32
a的ASCII码为97,A的ASCII码为65,两者相差32.
5. equals(Object anObject)
将此字符串与指定的对象进行比较,当且仅当参数不是null且是String对象表示与此对象相同的字符序列时,结果为true。
public class Test01 {
public static void main(String[] args) {
String text1 = "abcdefg";
String text2 = "AaBbCcdD";
System.out.println(text1.equals(text2));
}
}
复制代码
结果为:false
6. getBytes(Object anObject)
使用给定的charset将此String
编码为字节序列,将结果存储到新的字节数组中。
public class Test01 {
public static void main(String[] args) {
String a = "11 ";
String b = "好";
byte[] arr;
arr = a.getBytes();
for (byte x : arr){
System.out.print("x= " + x);
}
System.out.println();
for (byte y : arr){
System.out.print("y= " + y);
}
}
}
复制代码
结果为:x= 49x= 49x= 32 y= -28y= -67y= -96
1的ASCLL码为49,空格是32 。
这里声明了一个byte类型的数组array用来接受getBytes()方法返回类的字节数组。 发现 字符串 “好”转换成字节之后 数组array里面有两个元素 ,一个 -28,一个 -67,这是因为,字符“你”是一个中文字符,占用了两个字节,当将其转换为byte时高8位转换成一个字节显示为-28 , 低8位转换成一个字节显示为 -67 。
7. indexOf(int ch)
返回指定字符第一次出现的字符串中的索引。
public class Test01 {
public static void main(String[] args) {
String a = "abcdef ";
System.out.println(a.indexOf("d"));
}
}
复制代码
结果为:3
8. repeat(int count)
返回一个字符串,其值为此字符串的串联重复 count
次。
public class Test01 {
public static void main(String[] args) {
String a = "abcdefabddffe ";
System.out.println(a.repeat(2));
}
}
复制代码
结果为:abcdefabddffe abcdefabddffe
9. replace(char oldChar,char newChar)
返回从替换所有出现的导致一个字符串 oldChar
在此字符串 newChar
。
public class Test01 {
public static void main(String[] args) {
String a = "abcdefabddffe ";
System.out.println(a.replace("a","1"));
}
}
复制代码
结果为:1bcdef1bddffe
10. split(String regex)
将此字符串拆分为给定 regular expression的匹配 项 。
public class Test01 {
public static void main(String[] args) {
String a = "ab cde fab dd ffe ";
String[] arr = a.split(" ");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
复制代码
结果为:
ab cde fab dd ffe
11. subSequence(int beginIndex.int endIndex)
返回作为此序列的子序列的字符序列。
public class Test01 {
public static void main(String[] args) {
String a = "ab cde fab dd ffe ";
System.out.println(a.subSequence(0,5));
}
}
复制代码
结果为:ab cd
12.toLowerCase( )
使用默认语言环境的规则将此 String
所有字符转换为小写。
public class Test01 {
public static void main(String[] args) {
String a = "ABCdefG ";
System.out.println(a.toLowerCase());
}
}
复制代码
结果为:abcdefg
13.trim( )
返回一个字符串,其值为此字符串,删除了所有前导和尾随空格,其中space被定义为其代码点小于或等于 'U+0020'
(空格字符)的任何字符。
public class Test01 {
public static void main(String[] args) {
String a = " avdfds ";
System.out.println(a.trim());
}
}
复制代码
结果为:avdfds
14.valueOf(int i )
public class Test01 {
public static void main(String[] args) {
byte b = 32;
System.out.println(String.valueOf(b));
}
}
复制代码
结果为:10
这个10是字符类型的