1. 程式人生 > >PB中Replace的用法、如何替換字串中的空格

PB中Replace的用法、如何替換字串中的空格

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