这里用代码简单说明:
public static void main(String[] args) {
/* String name = "张三"; //张三
name += "1"; //张三1
System.out.println(name);*/
/*String s = "ab;cd;efg";
String[] array = s.split(";");
System.out.println(Arrays.toString(array));*/
/*
正则表达式
规则
是一种模式匹配语法, 可以使用一些符号,来定义一个规则,然后用规则与字符串进行匹配
验证输入的内容必须是一个手机号格式.
第一位1
第二位3 5 7 8 9
从第三位开始都是数字即可
拿到字符串每一位进行判断
正则表达式中符号:
\d 匹配的是0-9之间的数字
[2468]
[1-9]
*/
常用的方法(数字)
String p = "13345678909";
//boolean b = p.matches("\\d"); 匹配一位数字
//boolean b = p.matches("\\d*"); 任意个数字
//boolean b = p.matches("\\d{9}"); 恰好n次
//boolean b = p.matches("\\d{3,}");至少n次
// boolean b = p.matches("\\d{3,6}"); 至少n次 不超过m次
//boolean b = p.matches("[3578]*"); [3578] 匹配3或者5或者7或者8
//boolean b = p.matches("1[35789]\\d{9}");//手机号
boolean b = p.matches("[1-9]\\d{5,11}"); //qq号 数字 6-12位 0不能开头
常用方法(字母,及任意字符)
\d 匹配的是0-9之间的数字
[2468]
[1-9]
[a-z]
[A-z]
\\w == [A-z0-9_]
. 匹配任意的字符 \\.进行转义,只能匹配.
| X|Y 或
*/
String p = "ADJADds32@163.com";
//boolean b = p.matches("[a-z]*");
//boolean b = p.matches("[A-Z]*");
//boolean b = p.matches("[a-zA-Z]*");
//boolean b = p.matches("[A-z]*");
//boolean b = p.matches("\\w*"); //[A-z0-9_]
//邮箱格式 agdaysdfa@2tdas.com .com.cn
boolean b = p.matches("\\w{6,10}@\\w{2,6}\\.(com|com\\.cn)");
代码实例
public static void main(String[] args) {
/* String s = "ab3cd5ef6g";
String [] array = s.split("\\d");
System.out.println(Arrays.toString(array));*/
String s = "a2cd3ef4g";
String s1 = s.replace("c","C");
String s2 = s.replaceAll("\\d","C");
String s3 = s.replaceFirst("\\d", "C");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);