String replace Function

The replace() function allows you to replace all instances of a given character in a string with another character.

The String

replace()
function allows you to replace all instances of a given character with another character. You can also use
replace
to replace substrings of a String with a different substring.

Hardware Required

  • Arduino Board

Circuit

There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open.

circuit

Code

Caution: If you try to replace a substring that's more than the whole String itself, nothing will be replaced. For example:

1String stringOne = "<html><head><body>";
2String stringTwo = stringOne.replace("<html><head></head><body></body></html>", "Blah");

In this case, the code will compile, but

stringOne
will remain unchanged, since the replacement substring is more than the String itself.

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last Revision 2018/03/27 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.