Switch to standard view 
  Sybase logo
 
 
 



A String Occurrence Search and Replace Routine (PowerBuilder 7, 8 and 9)


/*THIS ROUTINE WORKS GENERICALLY FOR ANY STRING. FOR EXAMPLE, IF OLD_STR = "red" AND NEW_STR = "green", ALL OCCURRENCES OF "red" INSIDE OF MYSTRING WILL BE REPLACED WITH "green".*/


long      ll_StartPos = 1
string    ls_old_str, ls_new_str, ls_txt_str

ls_txt_str = sle_1.Text
ls_old_str = "red"
ls_new_str = "green"

// Find the first occurrence of 
ls_old_str ...
ll_StartPos = Pos(ls_txt_str, ls_old_str, ll_StartPos)

// Only enter the loop if you find 
ls_old_str ...

DO WHILE ll_StartPos > 0
    // Replace old_str with 
ls_new_str ...
    ls_txt_str = Replace(ls_txt_str, ll_StartPos, Len(ls_old_str), ls_new_str)

    // Find the next occurrence of 
ls_old_str
    ll_StartPos = Pos(ls_txt_str, ls_old_str, ll_StartPos + Len(ls_old_str))
LOOP

sle_2.Text = ls_txt_str



Back to Top
© Copyright 2010, Sybase Inc.