Skip to content

String Replace

The string::replace function replaces all occurrences of a specified substring within a string with a new substring.

Syntax

string::replace(<original>, <search>, <replace>)

Parameters

  • original: The input string where replacements will occur.
  • search: The substring to be replaced.
  • replace: The new substring that will replace occurrences of the search substring.

Returns

  • String: A new string with all occurrences of search replaced by replace.

Example

Try it out below!
string::replace("hello world", "world", "there")

This function is useful for modifying strings by substituting specific substrings with desired replacements.