如果我在 Java 中使用模式和匹配器,如何在找到子字符串后立即添加换行符? 更新
我找到了使用该replaceAll(String,String);
方法的解决方案
非常感谢大家,我的解决方案已格式化如下
String contentsToSearch = "This is an example string /* example comment in my string */ with some extra text following my inline comment";
//String regex = "[*/]" ;
//Pattern pattern = Pattern.compile(regex);
//Matcher matcher = pattern.matcher(contentsToSearch);
//if(matcher.find()){
// add a line break to the string i am searching
//}else{
// print out my string regularly
// System.out.println(contentsToSearch);
//}
contentsToSearch = contentsToSearch.replaceAll("\\*/", "\\*/\n");
System.out.println(contentsToSearch);