🚀Write In Front🚀
📝个人主页:令夏二十三
🎁欢迎各位→点赞👍 + 收藏⭐️ + 留言📝
📣系列专栏:Java
💬希望你看完之后,能对你有所帮助,不足请指正!共同学习交流 🖊
一、字符串
1. String
- String属于引用类型,也就是指向某一个地址,但也可以直接赋值:
String s1 = "Hello!";
- String也可以用 char[ ] 数组实现,因此有下面这个写法:
String s2 = new String(new char[] {'H', 'e', 'l', 'l', 'o', '!'});
- String中的元素不可通过任何方法单独修改。
2. 字符串比较
进行严格地判断是否相等,必须使用 equals( ) 方法:
public class String_test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
System.out.println(s1.equals(s2));
}
}
忽略大小写进行是否相等的判断,可以使用 equalsIgnoreCase( ) 方法:
public class String_test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
System.out.println(s1.equalsIgnoreCase(s2));
}
}
此外,对字符串的子串进行操作的方法还有很多,主要是搜索子串和提取子串。
使用 contains( ) 方法进行是否包含某个子串的判断:
public class String_test {
public static void main(String[] args) {
String s1 = "hello";
System.out.println(s1.contains("ll"));
}
}
搜索子串的方法:
// 取第一个遇到的目标字符的下标
"hello".indexOf("l");
// 2
// 取最后一个目标字符的下标
"hello".lastIndexOf("l");
// 3
// 判断字符串是否以某子串为开头
"hello".startsWith("he");
// true
// 判断字符串是否以某子串为结尾
"hello".endsWith("llo");
// true
提取子串的方法:
// 从下标为2的字符开始提取
"hello".substring(2);
// "llo"
// 从下标为2的字符开始提取到下标为4的字符之前(左闭右开)
"hello".substring(2, 4);
// "ll"
3. 去除首尾空白字符
使用 trim( ) 方法可以移除字符串首尾的空白字符,包含空格、制表符、换行符等:
public class String_test {
public static void main(String[] args) {
String s1 = " hello \t\t\n ";
System.out.println(s1.trim());
}
}
// hello
还有其他去除空白字符的方法:
// 不仅去除空白字符,还会去掉中文的空格字符
"hello ".strip();
// "hello"
//去除字符串前的空白字符
" hello ".stripLeading();
// "hello "
//去除字符串后的空白字符
" hello ".stripTrailing();
// " hello"
判断字符串是否为空和空白字符串的方法:
// 判断字符串长度是否为零
"".isEmpty();
// true
" ".isEmpty();
//false
// 判断字符串是否只包含空白字符
" \n \t".isBlank();
// true
" Hello".isBlank();
// false
4. 替换子串
在字符串中替换子串,常用的方法是根据字符或字符串进行全部替换:
String s = "hello";
// 把所有字符'l'替换成字符"o"
s.replace('l', 'o');
// "heooo"
// 把所有字符串"ll"替换成字符串"kk"
s.replace("ll", "kk");
// "hekko"
5. 分割字符串
分割一般使用 split( ) 方法:
public class String_test {
public static void main(String[] args) {
String s = "a,b,c,d";
String [] ss = s.split("\\,");
System.out.println(ss[0]);
}
}
6. 拼接字符串
拼接字符串使用 join( ) 方法,用指定的字符串连接字符串数组:
String[] arr = {"A", "B", "C"};
String s = String.join("aa", arr);
// "AaaBaaC"
7. 格式化字符串
字符串可以用 format( ) 方法进行传入参数,其中用到的占位符和C的格式化相似,如下:
%s | 字符串 |
%d | 整数 |
%x | 十六进制整数 |
%f | 浮点数 |
格式化字符串的示例如下:
public class String_test {
public static void main(String[] args) {
String s = "Hello %s, your score is %d!";
System.out.println(String.format(s, "Eric", 100));
}
}
8. 类型转换
使用 valueOf( ) 方法对所有其他类型直接转换为 String 类型:
public class String_test {
public static void main(String[] args) {
int a = 111;
String s = String.valueOf(a);
String s1 = "111";
System.out.println(s);
System.out.println(s1);
}
}
把字符串转换为整型:
int n = Interger.parseInt("123");
把字符串转换为字符数组:
char[] cs = "hello".toCharArray();
String s = new String(cs);
二、StringBuilder
Java中可以使用加号(+)随意拼接字符串,但是每次拼接字符串都会创建新的字符串对象,然后扔掉旧的字符串,这样效率太低,所以Java里面还有StringBuilder标准库,可以看作是一个类,用它来定义的字符串在使用append来拼接时,不会创建新的字符串对象:
StringBuild s = new StringBuild("123");
s.append("2");
System.out.println(s);
// 1232
String st = s.toString();
System.out.println(st);
// 1232
三、StringJoiner
拼接一连串字符串,可以使用StringJoiner,这个类提供了使用某一字符来拼接一系列字符串的功能。具体如下:
public class Main {
public static void main(String[] args) {
String[] names = {"Bob", "Alice", "Grace"};
var sj = new StringJoiner(", ");
for (String name : names) {
sj.add(name);
}
System.out.println(sj.toString());
}
}
此外,还可以指定拼接后的字符串的开头和结尾,只需要在StringJoiner的括号里加上这两个参数即可,如下:
public class Main {
public static void main(String[] args) {
String[] names = {"Bob", "Alice", "Grace"};
var sj = new StringJoiner(", ", "Hello ", "!");
for (String name : names) {
sj.add(name);
}
System.out.println(sj.toString());
}
}
注意,这两个类方法需要调用一个特殊的包:
import java.util.*;