Core Function SubStrReplace

From Sputnik Wiki
Jump to: navigation, search
SubStrReplace( <expression>, <replacement>, <start>, <length> )

Contents

Description

Replace text within a portion of a string.

Replaces a copy of string delimited by the (optionally) start and (optionally) length parameters with the string given in replacement.

Parameters

expression

The string to use.

replacement

The replacement string.

start

The position in subject to start searching.

If start is positive, the replacing will begin at the start'th offset into string.

If start is negative, the replacing will begin at the start'th character from the end of string.

length

If length is given and is positive, it represents the length of the portion of string which is to be replaced.

If it is negative, it represents the number of characters from the end of string at which to stop replacing.

If it is not given, then it will default to strlen( string ); i.e. end the replacing at the end of string.

Return Value

The result string is returned.

Remarks

None.

Example

$var = 'ABCDEFGH:/MNRPQR/';
say "Original: $var\n";
 
// These two examples replace all of $var with 'bob'.
say SubstrReplace($var, 'bob', 0);
say SubstrReplace($var, 'bob', 0, strlen($var));
 
// Insert 'bob' right at the beginning of $var.
say SubstrReplace($var, 'bob', 0, 0);
 
// These next two replace 'MNRPQR' in $var with 'bob'.
say SubstrReplace($var, 'bob', 10, -1);
say SubstrReplace($var, 'bob', -7, -1);
 
// Delete 'MNRPQR' from $var.
say SubstrReplace($var, '', 10, -1);
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox