So suppose I have:
String s = \"1479K\"; String t = \"459LP\";
and I want to return
String commonChars = \"49\";
String commonChars = s.replaceAll("[^"+t+"]","");
Note that you may need to escape special characters in t, e.g. using Pattern.quote(t) instead of t above.
t
Pattern.quote(t)