Replace
PowerScript function
Description
Replaces a portion of one string with another.
Syntax
|
1 |
Replace ( string1, start, n, string2 ) |
|
Argument |
Description |
|---|---|
|
string1 |
The string in which you want to replace characters with |
|
start |
A long whose value is the number of the first character |
|
n |
A long whose value is the number of characters you want to |
|
string2 |
The string that will replace characters in string1. The |
Return value
String. Returns the string with the characters replaced if it
succeeds and the empty string if it fails. If any argument’s value is
null, Replace returns null.
Usage
If the start position is beyond the end of the string, Replace
appends string2 to string1. If there are fewer characters after the start
position than specified in n, Replace replaces all the characters to the
right of character start.
If n is zero, then, in effect, Replace inserts string2 into
string1.
Examples
These statements change the value of Name from Davis to Dave:
|
1 2 3 |
string Name Name = "Davis" Name = Replace(Name, 4, 2, "e") |
This statement returns BABY RUTH:
|
1 |
Replace("BABE RUTH", 1, 4, "BABY") |
This statement returns Closed for the Winter:
|
1 |
Replace("Closed for Vacation", 12, 8, "the Winter") |
This statement returns ABZZZZEF:
|
1 |
Replace("ABCDEF", 3, 2, "ZZZZ") |
This statement returns ABZZZZ:
|
1 |
Replace("ABCDEF", 3, 50, "ZZZZ") |
This statement returns ABCDEFZZZZ:
|
1 |
Replace("ABCDEF", 50, 3, "ZZZZ") |
These statements replace all occurrences of red within the string
mystring with green. The original string is taken from the SingleLineEdit
sle_1 and the result becomes the new text of sle_1:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
long start_pos=1 string old_str, new_str, mystring mystring = sle_1.Text old_str = "red" new_str = "green" // Find the first occurrence of old_str. start_pos = Pos(mystring, old_str, start_pos) // Only enter the loop if you find old_str. DO WHILE start_pos > 0 // Replace old_str with new_str. mystring = Replace(mystring, start_pos, & Len(old_str), new_str) // Find the next occurrence of old_str. start_pos = Pos(mystring, old_str, & start_pos+Len(new_str)) LOOP sle_1.Text = mystring |
See also
Replace method for DataWindows in the section called “Replace” in DataWindow Reference.