一. String构造方法
java.lang.String类
String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。
因为 String 对象是不可变的,所以可以共享。
字符串底层就是一个数组,数组被final修饰,数组的地址值不能改变,所以字符串就是一个常量
private final char value[];
String类的构造方法:(IO流的时候用到)
String(String original)
//String s1 = new String("abc");
String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
根据编码表查询字节对应的编码,把字节转换为字符串 97-->a 65-->A
//byte[] bytes = {65,66,67,68,69};
//String s2 = new String(bytes);//s2:ABCDE
String(byte[] bytes, int offset, int length)把字节数组的一部分转换为字符串
/*参数: int offset:数组的开始索引 int length:转换的个数 */
//需求:把字节数组中的66和67,转换为字符串
//byte[] bytes = {65,66,67,68,69};
//String s3 = new String(bytes,1,2);//s3:BC
String(char[] value)把字符数组转换为字符串
//char[] chars = {'我','是','一','个','中','国','人'};
//String s4 = new String(chars);//s4:我是一个中国人
String(char[] value, int offset, int count)把字符数组的一部分转换为字符串
//char[] chars = {'我','是','一','个','中','国','人'};
//String s5 = new String(chars,4,3);//s5:中国人
//String s6 = "abc"; 底层 new char[]{'a','b','c'} //s6:abc
二. String成员方法
String类的常用成员方法:
1.String concat(String str) 将指定字符串连接到此字符串的结尾。 把两个字符串连接到一起,返回一个新的字符串 "ab".concat(s2);
2.boolean contains(CharSequence s) 判断字符串中是否包含指定的字符串;
2.boolean contains(String str) 判断字符串中是否包含指定的字符串;
3.boolean endsWith(String suffix) 判断字符串是否以指定的字符串而结尾;
4.boolean startsWith(String prefix) 判断字符串是否以指定的字符串而开头;
5.int indexOf(String str) 从前往后在字符串中查找另外一个字符串,找到了返回字符串对应的索引,找不到返回-1
6.int lastIndexOf(String str) 从后往前在字符串中查找另外一个字符串,找到了返回字符串对应的索引,找不到返回-1
7.String replace(CharSequence target, CharSequence replacement) 把字符串中所有的目标字符串,替换为新的字符串
7.String replace(String target, String replacement) 把字符串中所有的目标字符串,替换为新的字符串
8.String substring(int beginIndex) 从开始索引beginIndex开始截取字符串到字符串的末尾,截取一个新的字符串
9.String substring(int beginIndex, int endIndex) 从开始索引beginIndex到结束索引endIndex截取字符串;包含头,不包含尾
10.char[] toCharArray() 将此字符串转换为一个新的字符数组。
11.byte[] getBytes() 查询系统默认的编码表把字符串转换为字节数组
12.String toLowerCase() 把字符串中所有的英文字符转换为小写
13.String toUpperCase() 把字符串中所有的英文字符转换为大写
14.String trim() 去掉字符串两端的空格
15.String[] split(String regex) 根据指定的字符串对大的字符串进行切割,把大的字符串切割为多个小字符串,存储到一个数组中
三. join方法
Java8增强了String的方法,可以直接使用String.join合并List<String>,
第一个参数为连接字符串的字符,我这里用的是空格" ",第二个参数是待连接的字符串集合。
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "张三", "李四", "王五", "赵六");
System.out.println(String.join("@",list));//张三@李四@王五@赵六
String[] arr = {"a", "b", "c"};
System.out.println(String.join("@", arr));//a@b@c
四. String类转换
如何在字符串每个元素之间插入空格
第一种方法
String s = "abcdef";
s = s.replace("", " ").trim(); //--->a b c d e
s = s.replace(" ", "").trim(); //转换回来--->abcdef
System.out.println(s);
第二种方法
String s = "abcdef";
String s1 = "";
for (int x = 0; x < s.length(); x++) {
s1 += String.valueOf(s.charAt(x));
s1 += " ";
}
System.out.println(s1); //--->a b c d e
String[] s2 = s1.split(" ");//切割空格
System.out.println(s2);//[Ljava.lang.String;@1540e19d
StringBuilder sb = new StringBuilder();
for (int x = 0; x < s2.length; x++) {
sb.append(s2[x]);//遍历 添加
}
// String s3 = sb.toString();
String s3 = new String(sb);//转换字符串的两种方法
System.out.println(s3);//abcdef
//字符串转换为字符数组
String s = "abc def";
char[] chars = s.toCharArray();
System.out.println(chars[0]);//a
// 字符数组换为字符串
String s1 = new String(chars);
System.out.println(s1);//abc def
// 字符串转换为字符串数组
String[] array = s.split(" ");
System.out.println(array[0]);//abc
// 字符串数组转换为字符串
StringBuilder sb = new StringBuilder();
for (int x = 0; x < array.length; x++) {
sb.append(array[x]);
}
String s2 = sb.toString();
System.out.println(s2);//abcdef
//字符串转为字节数组
byte[] bytes = s.getBytes();
System.out.println(bytes[0]);//97
//字节数组转为字符串
String s3 = new String(bytes);
System.out.println(s3);//abc def
//字符串小写转大写
String s5 = ss.toUpperCase();
System.out.println(s5);//ABC DEF
//字符串大写转小写
String s6 = s5.toLowerCase();
System.out.println(s6);//abc def
//替换内容
String s7 = s6.replace("abc", "123");
System.out.println(s7);//123 def
//替换回来
String s8 = s7.replace("123", "abc");
System.out.println(s8);//abc def
五. 获取功能
String s = "abc def";
//获取字符长度
int length = s.length();
System.out.println(length);//7
//获取指定位置字符
char c = s.charAt(0);
System.out.println(c);//a
//查询小串第一次出现的位置(没有返回-1)
int index = s.indexOf("ef");
System.out.println(index);//5
//查询小串最后一次出现的位置(没有返回-1)
int index1 = s.lastIndexOf("ef");
System.out.println(index1);//5
//截取字符串指定开始位置
String s1 = s.substring(4);
System.out.println(s1);//def
//截取字符串指定开始位置和结束位置
String s2 = s.substring(4, 5);
System.out.println(s2);//d
//根据指定字符进行切割
String[] s3 = s.split(" ");
System.out.println(Arrays.toString(s3));//[abc, def]
六. 正则表达式
java.util.regex.Pattern:正则表达式的编译表示形式。 定义了正则表达式的一些规则
String类中和正则表达式相关的方法
boolean matches(String regex) 告知此字符串是否匹配给定的正则表达式。
String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。
String replaceAll(String regex, String replacement) 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
String s = "abc ccc ddd";
String[] split = s.split(" +");
System.out.println(Arrays.toString(split));
/*String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。*/
String z = s.replaceAll(" +", "z");
System.out.println(z);
使用字符串调用matches方法进行校验
正则表达式-字符类:[]表示一个区间,区间的范围可以自己定义
语法示例:
1. [abc]:代表a或者b,或者c字符中的一个。
2. [^abc]:代表除a,b,c以外的任何字符。
3. [a-z]:代表a-z的所有小写字符中的一个。
4. [A-Z]:代表A-Z的所有大写字符中的一个。
5. [0-9]:代表0-9之间的某一个数字字符。
6. [a-zA-Z0-9]:代表a-z或者A-Z或者0-9之间的任意一个字符。
7. [a-dm-p]:a 到 d 或 m 到 p之间的任意一个字符
//验证字符串是否以h开头,d结尾,中间是aeiou中的某一个字符
String regex = "h[aeiou]d";
System.out.println("had".matches(regex));//true
System.out.println("hsd".matches(regex));//false
//验证字符串是否以h开头,d结尾,中间不是aeiou中的某一个字符
String regex1 = "h[^aeiou]d";
System.out.println("had".matches(regex1));//false
System.out.println("hsd".matches(regex1));//true
//验证字符串是否以a-z任何一个小写字符开头,后面跟ad
String s = "[a-z]ad";
System.out.println("aad".matches(s));//true
System.out.println("asd".matches(s));//false
正则表达式-逻辑运算符
语法示例:
1. &&:并且 多个条件同时满足
2. || :或者 满足其中一个条件即可
//要求字符串是小写[a-z]和不是字符[^aeiou]开头,后面跟ad
String regex = "[[a-z]&&[^aeiou]]ad";
System.out.println("sad".matches(regex));//true
System.out.println("aad".matches(regex));//false
//要求字符串是aeiou中的某个字母开头,后面跟ad
String regex1 = "[a|b|c|d]ad";
System.out.println("sad".matches(regex1));//false
System.out.println("aad".matches(regex1));//true
正则表达式-预定义字符
语法示例:
1. "." : 匹配任何字符。(重点)
2. "\\d":任何数字[0-9]的简写 (重点)
3. "\\D":任何非数字[^0-9]的简写
4. "\\s": 空白字符(空格):[ \t\n\x0B\f\r] 的简写
5. "\\S": 非空白字符:[^\s] 的简写
6. "\\w":单词字符:[a-zA-Z_0-9]的简写(重点)
7. "\\W":非单词字符:[^\w]
注意:
\本身就是转义字符,写正则表达式的时候,必须写\\,把有特殊含义的\转义为一个普通的\使用 //验证字符串是否是3位数
String regex = "[0-9][0-9][0-9]";
regex = "\\d\\d\\d";
System.out.println("123".matches(regex));//true
//验证手机号:1开头 第二位358 之中的一个 剩下9位全是数字
String regex1 = "1[358][0-9]{9}";
System.out.println("15669321457".matches(regex1));//true
//验证是否以h开头以d结尾中间是任何字符
String regex2 = "h.d";
System.out.println("had".matches(regex2));//true
System.out.println("h&d".matches(regex2));//true
/*验证str是否是:had.
* 注意:.被转义成普通字符*/
String str = "had\\.";
System.out.println("had.".matches(str));//true
正则表达式-数量词
语法示例:
1. X? : 0次或1次
2. X* : 0次到多次 任意次
3. X+ : 1次或多次
4. X{n} : 恰好n次 X==n次
5. X{n,} : 至少n次 X>=n次
6. X{n,m}: n到m次(n和m都是包含的) n=<X<=m
//验证字符串是否是三位数
String regex = "\\d{3}";
System.out.println("111".matches(regex));//true
System.out.println("11123".matches(regex));//false
//验证字符串是否为多位数字:1次以上
String regex2 = "\\d+";
System.out.println("123125555".matches(regex2));//true
//验证手机号:1开头 第二位358 之中的一个 剩下9位全是数字
String regex3 = "1[358][0-9]{9}";
System.out.println("15669321457".matches(regex3));//true
//验证QQ号: 第一位不能是0 ,5-15位,全部都是数字
String regex4 = "[1-9]\\d{4,14}";
System.out.println("1357234".matches(regex4));//true
System.out.println("1357".matches(regex4));//false
正则表达式-分组括号( )
//效验字符串"abc"可以出现任意次
String regex = "(abc)*";
System.out.println("abcabcabcabc".matches(regex));//true
System.out.println("abcdefabc".matches(regex));//false