-
创建初始字符串
String s1 = "find a job"; String s2 = "find a Good job"; char[] chars = {'f', 'i', 'n', 'd', ' ', 'a', ' ', 'j', 'o', 'b'};
-
获取字符串长度
System.out.println(s1.length());
-
获取字符串指定索引的字符
System.out.println(s1.charAt(0));
-
获取指定字符的索引
System.out.println(s1.indexOf("a"));
-
获取指定字符的索引,从指定位置考试查找
System.out.println(s1.indexOf("a", s1.indexOf("a") + 1));
-
获取指定字符最后一次索引
System.out.println(s1.lastIndexOf("a"));
-
判断字符串是否为空
System.out.println(s1.isEmpty());
-
判断字符串是否以指定字符开头
System.out.println(s1.startsWith("find"));
-
判断字符串是否以指定字符结尾
System.out.println(s1.endsWith("1b"));
-
比较字符串是否相等
System.out.println(s1.equals("find a job"));
-
比较字符串是否相等,忽略大小写
System.out.println(s1.equalsIgnoreCase("find a job"));
-
将字符数组转化为字符串
String s = String.valueOf(chars);
-
使用指定字符链接字符串
String.join("-", "a", "b", "c");
-
将字符串转化为字节数组
System.out.println(s1.getBytes());
-
将字符串转化为字符数组
System.out.println(s1.toCharArray());
-
替换指定字符
System.out.println(s1.replace("a", "b"));
-
切割字符串成数组
System.out.println(s1.split("a"));
-
获取子串
System.out.println(s1.substring(5)); System.out.println(s1.substring(5, 10));
-
去除字串两端空格
System.out.println(s1.trim());
-
将字符串转为大写或者小写
System.out.println(s1.toUpperCase()); System.out.println(s1.toLowerCase());
-
对字符串进行指定比较
System.out.println(s1.compareTo("find a job"));
-
使用指定的格式字符串和参数返回格式化的字符串
System.out.println(String.format("name=%s,age=%d", "张三", 20));
常用类-String类
![](https://img-home.csdnimg.cn/images/20240711042549.png)