Return Values
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
:
string Name
Name = "Davis"
Name = Replace(Name, 4, 2, "e")
This statement returns BABY RUTH
:
Replace("BABE RUTH", 1, 4, "BABY")
This statement returns Closed for the Winter
:
Replace("Closed for Vacation", 12, 8, "the Winter")
This statement returns ABZZZZEF
:
Replace("ABCDEF", 3, 2, "ZZZZ")
This statement returns ABZZZZ
:
Replace("ABCDEF", 3, 50, "ZZZZ")
This statement returns ABCDEFZZZZ
:
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:
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
PB中如何删除字符串中的空格和回车符
1,将字符串置入一 char 数组,
2,循环该 char 数组,将 asc 数值为非空格和回车的字符装入另一新 char 数组。
3,string 该新的 char 数组成一个字符串即可。
以上三步可以写成一个函数。
string ls_str = ' ' //删除空格为例,回车为~r~n
string ls_data = 'sdlkfjsdlkfsd sdlfsdjfklsdsdkjfsdlf sd sdflskdjflsd'
long ll_pos
ll_pos = pos(ls_data, ls_str)
do while ll_pos > 0
ls_data = left(ls_data, ll_pos - 1) + mid(ls_data, ll_pos + len(ls_str))
ll_pos = pos(ls_data, ls_str)
loop
string ls_str = '~r~n'
string ls_data = 'sdlkfjsdlkfsd sdlfsdjfklsdsdkjfsdlf sd sdflskdjflsd'
long ll_pos
ll_pos = pos(ls_data, ls_str)
do while ll_pos > 0
ls_data = left(ls_data, ll_pos - 1) + mid(ls_data, ll_pos + len(ls_str))
ll_pos = pos(ls_data, ls_str)
loop