package course.api.string; import java.util.Arrays; /** * @author WanJl * @version 1.0 * @title StringTest02 * @description * @create 2026/7/27 */ public class StringTest02 { public static void main(String[] args) { String str1 = " hello world "; String str2 = " Hello World "; //比较字符串是否相同,比较的是内容,区分大小写。 boolean b = str1.equals(str2); System.out.println(b); //比较字符串是否相同,比较的是内容,不区分大小写 boolean b1 = str1.equalsIgnoreCase(str2); System.out.println(b1); ////返回字符串的长度 int length = str1.length(); System.out.println(length); //获取指定字符,返回索引值为2的那个字符。 char c = str1.charAt(2); System.out.println(c); //截取字符串[0,5] 包含 0 索引,不包含5索引 String subStr = str1.substring(0, 5); System.out.println(subStr); //查找子字符串位置,没有找到返回-1,返回这个子字符串在str1字符中第一次出现的位置 int i = str1.indexOf("lo"); System.out.println(i); // 判断str1字符串中是否包含 world 字符串 boolean b2 = str1.contains("world"); System.out.println(b2); //判断str1字符串是否以he开头 str1.startsWith("he"); //判断str1字符串是否以he结尾 str1.endsWith("he"); //字符串转大写 String upperCase = str1.toUpperCase(); System.out.println(upperCase); //字符串转小写 String lowerCase = str1.toLowerCase(); System.out.println(lowerCase); //去掉首尾空格 String trim = str1.trim(); System.out.println(trim); //去除全角/半角空格 JDK11+才有 String strip = str1.strip(); System.out.println(strip); //把旧的字符串(第1个参数)替换为新的字符串(第2个参数) String replace = str1.replace("world", "java"); System.out.println(replace); //按照 字符 , 把字符串分割成字符串数组 String s3="王艳阳,赵援翔,王嘉宇,王德昱,刘铭达,吴限,陈健,白立伟,田烁楠,王贺,赵颜,马莺莺,那洪舰,谷娜颖,孙宁,李时光,李昌龙,符荣里,吴壮,司一祺,徐振国"; String[] split = s3.split(","); System.out.println(Arrays.toString(split)); //判断字符串是否为空(长度为0) boolean empty = str1.isEmpty(); System.out.println(empty); // 判断字符串是否为空或空白字符--JDK11+ boolean blank = str1.isBlank(); System.out.println(blank); //使用指定的字符,讲后面的每个字符串进行拼接 String join = String.join("-", "A", "B", "C", "张三", "李四"); System.out.println(join); } }