1.字符串构造代码语言:javascript复制public static void main(String[] args) {
// 使用常量串构造
String s1 = "hello bit";
System.out.println(s1);
// 直接newString对象
String s2 = new String("hello bit");
System.out.println(s1);
// 使用字符数组进行构造
char[] array = {'h','e','l','l','o','b','i','t'};
String s3 = new String(array);
System.out.println(s1);
} String是引用类型,内部并不存储字符串本身
代码语言:javascript复制public static void main(String[] args) {
// s1和s2引用的是不同对象 s1和s3引用的是同一对象
String s1 = new String("hello");
String s2 = new String("world");
String s3 = s1;
System.out.println(s1.length()); // 获取字符串长度---输出5
System.out.println(s1.isEmpty()); // 如果字符串长度为0,返回true,否则返回false
}2.String对象的比较 2.1. ==比较是否引用同一个对象 对于内置类型,==比较的是变量中的值;对于引用类型==比较的是引用中的地址
代码语言:javascript复制public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
// 对于基本类型变量,==比较两个变量中存储的值是否相同
System.out.println(a == b); // false
System.out.println(a == c); // true
// 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // false
System.out.println(s1 == s4); // true
}2.2.boolean equals(Object anObject) 方法代码语言:javascript复制public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("Hello");
// s1、s2、s3引用的是三个不同对象,因此==比较结果全部为false
System.out.println(s1 == s2); // false
System.out.println(s1 == s3); // false
// equals比较:String对象中的逐个字符
// 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true
// s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
}2.3.int compareTo(String s) 方法 与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:
1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
代码语言:javascript复制public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("abc");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s3)); // 相同输出 0
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
} 2.4.int compareToIgnoreCase(String str) 方法:与compareTo方式相同,但是忽略大小写比较代码语言:javascript复制public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("ABc");
String s4 = new String("abcdef");
System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1
System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3
}3.字符串查找方法
功能
char charAt(int index)
返回index位置上字符,如果index为负数或者越界,抛出 IndexOutOfBoundsException异常
int indexOf(int ch)
返回ch第一次出现的位置,没有返回-1
int indexOf(int ch, int fromIndex)
从fromIndex位置开始找ch第一次出现的位置,没有返回-1
int indexOf(String str)
返回str第一次出现的位置,没有返回-1
int indexOf(String str, int fromIndex)
从fromIndex位置开始找str第一次出现的位置,没有返回-1
int lastIndexOf(int ch)
从后往前找,返回ch第一次出现的位置,没有返回-1
int lastIndexOf(int ch, int fromIndex)
从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返 回-1
int lastIndexOf(String str)
从后往前找,返回str第一次出现的位置,没有返回-1
int lastIndexOf(String str, int fromIndex)
从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返 回-1
代码语言:javascript复制public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); // 'b'
System.out.println(s.indexOf('c')); // 6
System.out.println(s.indexOf('c', 10)); // 15
System.out.println(s.indexOf("bbb")); // 3
System.out.println(s.indexOf("bbb", 10)); // 12
System.out.println(s.lastIndexOf('c')); // 17
System.out.println(s.lastIndexOf('c', 10)); // 8
System.out.println(s.lastIndexOf("bbb")); // 12
System.out.println(s.lastIndexOf("bbb", 10)); // 34.转化4.1.数值和字符串转化 valueOf()方法代码语言:javascript复制public static void main(String[] args) {
// 数字转字符串
String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
String s4 = String.valueOf(new Student("Hanmeimei", 18));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("=================================");
// 字符串转数字
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
}4.2.大小写转化方法
功能
toUpperCase()
小写转大写
toLowerCase()
大写转小写
代码语言:javascript复制public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// 小写转大写
System.out.println(s1.toUpperCase());
// 大写转小写
System.out.println(s2.toLowerCase());
}4.3.字符串转数组代码语言:javascript复制public static void main(String[] args) {
String s = "hello";
// 字符串转数组
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]);
}
System.out.println();
// 数组转字符串
String s2 = new String(ch);
System.out.println(s2);
} 4.4.格式化代码语言:javascript复制public static String format(String format, Object... args)代码语言:javascript复制public class StringFormatExample {
public static void main(String[] args) {
// 使用 String.format() 格式化字符串
String name = "Alice";
int age = 30;
double salary = 5000.50;
// 格式化字符串,%s 表示字符串,%d 表示整数,%.2f 表示保留两位小数的浮点数
String formattedString = String.format("Name: %s, Age: %d, Salary: $%.2f", name, age, salary);
String s = String.format("%d-%d-%d", 2019, 9,14);
// 打印格式化后的字符串
System.out.println(formattedString); // 输出:Name: Alice, Age: 30, Salary: $5000.50
System.out.println(s);// 2019-9-14
}
}5.字符串替换 使用一个指定的新的字符串替换掉已有的字符串
由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串
方法
功能
String replaceAll(String regex, String replacement)
替换所有的指定内容
String replaceFirst(String regex, String replacement)
替换收个内容
代码语言:javascript复制String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));//he__oworld
System.out.println(str.replaceFirst("l", "_"));//he_loworld6.字符串拆分 可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。
方法
功能
String[] split(String regex)
将字符串全部拆分
String[] split(String regex, int limit)
将字符串以指定的格式,拆分为limit组
代码语言:javascript复制public class StringSplitComplexExample {
public static void main(String[] args) {
// 原始字符串,使用空格、逗号或分号分隔
String originalString = "apple banana,orange;grape";
// 使用正则表达式拆分字符串,匹配空格、逗号或分号(一个或多个)
String[] splitResult = originalString.split("[\\s,;]+");
// 打印拆分后的结果
for (String fruit : splitResult) {
System.out.println(fruit);
}
// 输出:
// apple
// banana
// orange
// grape
}
} 正则表达式"[\\s,;]+"匹配一个或多个空格(\\s)、逗号(,)或分号(;),并根据这些匹配项来拆分字符串。
代码语言:javascript复制String str = "hello world hello world" ;
String[] result = str.split(" ",2) ;
for(String s: result) {
System.out.println(s);
} 有些特殊字符作为分割符可能无法正确切分, 需要加上转义.
1. 字符"|","*","+"都得加上转义字符,前面加上 "\\" .
2. 而如果是 "\" ,那么就得写成 "\\\\" .
3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符.
7.字符串截取 从一个完整字符串中截取部分内容
方法
功能
String substring(int beginIndex)
从指定索引截取到结尾
String substring(int beginIndex, int endIndex)
截取部分内容
代码语言:javascript复制String str = "helloworld" ;
System.out.println(str.substring(5));//world
System.out.println(str.substring(0, 5));//hello 1. 索引从0开始
2. 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标
8. trim()方法 trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等)
代码语言:javascript复制String str = " hello world " ;
System.out.println("["+str+"]"); //[ hello world ]
System.out.println("["+str.trim()+"]");//[hello world]