如何获得所需的输出?
输入字符串-
"Software Industry. software Industry. SOFTWARE INDUSTRY. software industry. "被搜查的消息-
"software industry"产出-
"*Software Industry*. *software Industry*. *SOFTWARE INDUSTRY*. *software industry*. "总之,我必须找到一个短语,用开始和结束星号(*)替换相同的短语,忽略这个情况。
发布于 2012-10-17 06:25:21
您可以将replaceAll与(?i)一起使用,这将执行case-insensitive替换。
String str = "Software Industry. software Industry. SOFTWARE INDUSTRY. "+
"software industry. "
str = str.replaceAll("(?i)(software industry)", "\\*$1\\*");发布于 2012-10-17 06:25:08
为此,可以使用模式匹配(Regex)。
首先使用组提取短语并将其存储在字符串或任何其他数据结构中。然后使用替换函数来实现输出。
你可以参考这篇文章:http://www.vogella.com/articles/JavaRegularExpressions/article.html
发布于 2012-10-17 06:27:30
您也可以尝试这样的方法:
String str=new String("Software Industry. software Industry. SOFTWARE INDUSTRY. software industry.");
str=Pattern.compile("(software industry)",Pattern.CASE_INSENSITIVE).matcher(str).replaceAll("*$1*");https://stackoverflow.com/questions/12928265
复制相似问题