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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值