如何获得所需的输出?
输入字符串:
" software Company(1920 of 2012)(sb.2)(sb.3) IBM on established (2009)"
要搜索的字词 -
"software company(1920 of 2012)"
输出
*software Company(1920 of 2012)*(sb.2)(sb.3) IBM on established (2009)
简而言之,我必须找到一个短语并用星号(*)开头和结尾替换相同的短语,忽略案例和匹配精确世界我
答案 0 :(得分:0)
您可以使用String#replaceAll()
String original = " software Company(1920 of 2012)(sb.2)(sb.3) IBM on established (2009)";
String subString = "software company(1920 of 2012)";
System.out.println("New String is: " + original.replaceAll("(?i)" + Pattern.quote(subString), "*$0*"));
添加(?i)
使其不区分大小写,Pattern#quote()
用于转义子字符串中的特殊正则表达式字符。
打印:
New String is: *software Company(1920 of 2012)*(sb.2)(sb.3) IBM on established (2009)
答案 1 :(得分:0)
正则表达式将满足您的需求,请查看以下链接regular expression tutorial