What is the difference between replace() and replaceAll()?

Asked

Viewed 2,048 times

8

I was reading the documentation to properly develop a mask and save without it.

However I came across this doubt of methods replace and replaceAll for my string.

What is the difference between the two, apparently both serve the same purpose.

  • 4

    The difference is that the former uses a sequence of chars to make the substitution, the latter uses regex. Both are for the same purpose, only changes that.

  • @diegofm less bad so there is no danger to use one or the other

1 answer

10


Yes, both serve the same purpose. The replace() replaces all occurrences of a character or a sequence of char in a string, while the replaceAll() does the same based on a regular expression pattern.

  • 2

    replace uses replaceAll until, according to source code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.