Faster alternatives to replace method in a Java String?

后端 未结 10 1414
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 11:07

The fact that the replace method returns a string object rather than replacing the contents of a given string is a little obtuse (but understandable when you know that strin

10条回答
  •  猫巷女王i
    2020-12-14 11:47

    Just get the char[] of the String and iterate through it. Use a temporary StringBuilder.

    Look for the pattern you want to replace while iterating if you don't find the pattern, write the stuff you scanned to the StringBuilder, else write the replacement text to the StringBuilder.

提交回复
热议问题