字符串类
String类,最终类,不能被继承
声明
String s1 = "hello"; String s2 = String.valueOf(200);
Jdk15 新添加字符块声明
String s1 = """ 一个字符块 可以换行 this.name = name; this.score = score; String s1 = "200"; String s2 = String.valueOf(200); String s3 = 200+""; } """;
字符串常用方法:
字符串比较
字符串比较有两个常用方法
equals和equalsIgnoreCase
== 比较字符串地址
equals 比较两个字符串内容,区分大小写
equalsIgnoreCase 比较两个字符串内容,不区分大小写
package str;
public class Str2 {
public static void main(String[] args) {
String s1 = "ok";
String s2 = "ok";
String s3 = new String("ok");
String s4 = new String("ok");
String s5 = new String("OK");
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s3 == s4);
System.out.println(s2.equals(s3));
System.out.println(s3.equals(s4));
System.out.println(s4.equals(s5));
System.out.println(s4.equalsIgnoreCase(s5));
}
}
字符串长度和字节个数
length(),返回字符串长度 int类型,是String类的一个方法
getBytes,将字符串转化为字节数组,使用length统计个数
String s1 = "hello world 中谷";
System.out.println("s1字符串的长度是:"+s1.length());
//在UTF-8编码中,汉字占用3个字节
System.out.println("s1字符串转为字节数组的长度是:"+s1.getBytes().length);
清除空格
trim()方法
清除字符串 左边 或者右边 连续的空格
String s1 = " hello w o r l d "; System.out.println(s1); String s2 = s1.trim(); System.out.println(s2); System.out.println("s1的长度:"+s1.length()); System.out.println("s2的长度:"+s2.length());
replace()
方法
替换字符串中所有与第一个参数相同的,替换为第二个参数
会返回一个新字符串
replaceFirst,仅替换字符串中第一个匹配的字符
replaceAll,被替换的字符可以是表达式(正则表达式)
String s1= " hhellohh whhorl d "; System.out.println(s1); String s2 = s1.replace("hh","+javagood||"); String s3 = s1.replaceAll("hh","/bad||"); String s4 = s1.replaceFirst("hh","*true||"); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println("s1的长度:" + s1.length()); System.out.println("s2的长度:" + s2.length()); System.out.println("s3的长度:" + s3.length()); System.out.println("s4的长度:" + s4.length());
字符串重复(复制)
repeat()
java 11的新方法
字符串重复(复制)方法,前面写字符串,括号内写重复次数
String s2 = "**"; System.out.println(s2.repeat(5)); System.out.println("-".repeat(9));
将字符串转成数组
split()
toCharArray()
将字符串保存成数组,按照括号内的字符进行分割
String s1 = "日,一,二,三,四,五,六"; String[] s2 = s1.split(""); String[] s3 = s1.split("二,"); System.out.println(s2[1]+"****"+s2[2]); System.out.println(s2[0]);
toCharArray()方法,可以将字符串转化为char数组
charAt()方法,从字符串中取出一个值,返回char类型
System.out.println(s1); char[] c1 = "abcdefghijklmnopqrstuvwxyz".toCharArray(); System.out.println(c1[3]); char c2 = s1.charAt(4); System.out.println(c2);
查验子串
contains()
查找字符串中是否有需要查询的子串,如果有,返回true,没有,返回false
仅限字符串,不能检查正则表达式
String s1 = "hello java bye php"; boolean b1 = s1.contains("php"); boolean b2 = s1.contains("javab"); System.out.println(b1); System.out.println(b2);
indexOf
查找字符串中是否有需要查询的子串,如果有,返回下标,如果没有,返回-1
从左到右 indexOf() 参数为字符或者ASCII值
从右到左 lastIndexOf()
String s1 = "hello java 123456 ByE lkjh"; System.out.println(s1.indexOf("a")); System.out.println(s1.indexOf("java")); System.out.println(s1.indexOf("c")); System.out.println(s1.lastIndexOf("a"));
截取子串
substring()
substring() 可以有一个int参数a,或者两个int参数x,y,
代表从[a]开始截取,或者从[x]截取到[y-1]
String s2 = "e:/user/local/pro/img/abc.jpg"; System.out.println(s2.substring(s2.lastIndexOf("."))); String s3 = s2.substring(0,s2.lastIndexOf("/")); System.out.println(s3);
字符串大小写转换
toLowerCase()
toUpperCase()
toLowerCase() 将字符串全部转换为小写字母
toUpperCase() 将字符串全部转换为大写字母
String s4 = "Hello PatTie"; System.out.println(s4); System.out.println(s4.toLowerCase()); System.out.println(s4.toUpperCase());
判断开头结尾字符
endsWith()
startWith()
startWith() 判断开头,返回true 或者false
endsWith() 判断结尾,返回true 或者false
String s1 = "hello POISH"; System.out.println(s1.endsWith("he")); System.out.println(s1.startsWith("he"));
字符串拼接
concat()
在指定字符串后拼接括号内字符串
返回一个新字符串
String s1 = "hello POISH"; String s2 = s1.concat("1111122222qqqqq33333"); System.out.println(s2);