-1

How do I get the required output?

input string :

 " software Company(1920 of 2012)(sb.2)(sb.3) IBM on established (2009)"

word to be searched -

"software company(1920 of 2012)"

Output

*software Company(1920 of 2012)*(sb.2)(sb.3) IBM on established (2009) 

In short I have to find a phrase and replace the same phrase with starting and ending with asterisk (*), IGNORING THE CASE, and MATCH EXACT WORLD i

Keppil
  • 45,603
  • 8
  • 97
  • 119

1 Answers1

0

You can do this easily using 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*"));

The (?i) is added to make it case insensitive, and the Pattern#quote() is used to escape special regex characters in your substring.

This prints:

New String is:  *software Company(1920 of 2012)*(sb.2)(sb.3) IBM on established (2009)
Keppil
  • 45,603
  • 8
  • 97
  • 119