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>";
2stringOne.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

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.