Tutorial

Getting Started with Macros

Macro Editor

User Interface

Menus and Toolbar

Editing Macros

Macro Debugging

How To

Using variables

Finding and modifying objects

Creating new page content

Asking for user input

Storing persistent data

Using binary data

Sorting objects

Macro menus

Bulleted and numbered lists

Sample Macros

Concepts

Expressions

Objects

Properties

Variables

Data Types

Arrays

Functions

Literals

Operators

Comments

Last updated on: September 02, 2024
Also available as a single HTML file

Onetastic Macro Documentation > Functions > String Functions > String_Replace

String_Replace

Finds occurences of a search string in the given string and replaces them with a replacement string and returns the resulting string.

Syntax

String String_Replace(
	String string, 
	Mixed search, 
	Mixed replacement, 
	Bool ignorecase, 
	out Numeric count)

Parameters

String string
String being searched and replaced on.
Mixed search
The value being searched for. This can be a single string or an array of strings. If it is a single string, replacement should also be a single string and, each occurence of that string is replaced with the replacement string. If it is an array of strings each value is replaced starting from the first element in the array. The replacement can be a single string or an array of strings in this case.
Mixed replacement
The value to replace the search string. This can be a single string or an array of strings. If it is a single string, each occurence of the search string or strings is replaced with the replacement string. If it is an array of strings each value in the array of search strings is replaced with the corresponding entry in the replacement array.
Bool ignorecase
Ignores the case of the search string(s) if set to true.
out Numeric count
The number of replacements performed will be stored in this variable.

Examples

$string = "abcdefghijABCDEFGHIJ" // Not found String_Replace($string, "345", "{}", true, $count) // Returns "abcdefghijABCDEFGHIJ" // $count is 0 // case Insensitive String_Replace($string, "def", "{}", true, $count) // Returns "abc{}ghijABC{}GHIJ" // $count is 2 // case Sensitive String_Replace($string, "def", "{}", false, $count) // Returns "abc{}ghijABCDEFGHIJ" // $count is 1 String_Replace($string, Array("a", "d", "H"), "{}", true, $count) // First replaces "a" with "{}" // Result is "{}bcdefghij{}BCDEFGHIJ // Then replaces "d" with "{}" // Result is "{}bc{}efghij{}BC{}EFGHIJ // Then replaces "H" with "{}" // Result is "{}bc{}efg{}ij{}BC{}EFG{}IJ // $count is 6 String_Replace($string, Array("a", "}b", ")c"), Array("{}", "()", "[]"), true, $count) // First replaces "a" with "{}" // Result is "{}bcdefghij{}BCDEFGHIJ" // Then replaces "}b" with "()" // Result is "{()cdefghij{()CDEFGHIJ" // Then replaces ")c" with "[]" // Result is "{([]defghij{([]DEFGHIJ" // $count is 6 String_Replace($string, Array("ab", "xy", "jA"), Array("XY", "ZW"), false, $count) // First replaces "ab" with "XY" // Result is "XYcdefghijABCDEFGHIJ" // Then replaces "xy" with "ZW" - not found because it is case sensitive // Result is "XYcdefghijABCDEFGHIJ" // Then replaces "jA" with empty string "" // Result is "XYcdefghiBCDEFGHIJ" // $count is 2 String_Replace($string, "ab", Array("XY", "ZW"), true, $count) // Replaces "ab" with "XY". "ZW" is ignored // Returns "XYcdefghijXYCDEFGHIJ" // $count is 2

Reference

Statements

Hierarchy Objects

Page Objects

Other Objects

Functions

Array Functions

Color Functions

Data Store Functions

Date/Time Functions

Dialog Box Functions

Macro Execution Functions

Macro Menu Functions

Object Functions

Special Functions

String Functions

Window Functions