How do I find the characters common to two strings in Java using single replaceAll?

后端 未结 4 738
时光说笑
时光说笑 2021-01-12 16:25

So suppose I have:

String s = \"1479K\";
String t = \"459LP\";

and I want to return

String commonChars = \"49\";

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-12 17:22

    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.

提交回复
热议问题