gpt4 book ai didi

java - 子字符串后换行

转载 作者:行者123 更新时间:2023-12-01 18:48:24 25 4
gpt4 key购买 nike

如果我在 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);

最佳答案

你可以简单地这样做:

contentsToSearch = contentsToSearch.replaceAll("(/\\*|\\*/)", "$1\n")

关于java - 子字符串后换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16703645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com