Replacing multiple substrings in Java when replacement text overlaps search text

前端 未结 5 711
囚心锁ツ
囚心锁ツ 2021-01-12 20:54

Say you have the following string:

cat dog fish dog fish cat

You want to replace all cats with dogs, all do

5条回答
  •  鱼传尺愫
    2021-01-12 21:42

    It seems StringUtils.replaceEach in apache commons does what you want:

    StringUtils.replaceEach("abcdeab", new String[]{"ab", "cd"}, new String[]{"cd", "ab"});
    // returns "cdabecd"
    

    Note that the documenent at the above links seems to be in error. See comments below for details.

提交回复
热议问题