java字符串操作:匹配、替换、萃取、分割拆分

来源:互联网 发布:益思留学 知乎 编辑:程序博客网 时间:2024/06/05 09:49

java字符串操作:匹配、替换、萃取、分割拆分

package com.java.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {
public static void main(String[] args){
String str="78965djfkdj3456fereir57657orrrrr";
//匹配
boolean flag=str.matches(".*rrr.*");
System.out.println("是否匹配:"+flag);
//替换
String str1=str.replace("\\r","w");
System.out.println("替换后的字符串为:"+str1);
//萃取
Pattern strp=Pattern.compile("\\d+");
Matcher mc=strp.matcher(str);
System.out.print("萃取的数为:");
while(mc.find()){
System.out.print(" "+mc.group());
}
System.out.println("");
//分割拆分
String[] sp=str.split("\\d+");
System.out.print("分割拆分后为:");
for(int i=0;i<sp.length;i++){
System.out.print(sp[i]+" ");
}
}
}

打印结果如下:


阅读全文
0 0