- 替换字符串中指定位置的字符
- 判断指定字符串是否全部匹配正则表达式
public class StringMethod03 {
@Test
public void test(){
String s="恰似人间惊鸿晏";
System.out.println(s.replace('人','在'));//指定字符替换指定字符串中的某字符
System.out.println(s.replace("人间","四月"));//指定字符串替换某字符串中的字符串
String s1="123hello456world";
System.out.println(s1.replaceAll("\\d+",",").replaceAll("^,",""));// 指定字符串替换正则表达式中的内容
String s2="1234";
System.out.println(s2.matches("\\d+"));//判断指定字符串是否全部匹配正则表达式 返回true or false
String s3="0791-134566";
System.out.println(s3.matches("0791-\\d{7,8}")); //判断这是否是一个固定电话
}
}
输出结果
恰似在间惊鸿晏
恰似四月惊鸿晏
hello,world
true
false