PowerBuilder Function Replace String gf_global_replace
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function Name : gf_global_replace // Argument Name : as_source, Arg Type : String, Pass By : Value // as_old, Arg Type : String, Pass By : Value // as_new, Arg Type : String, Pass By : Value // ab_ignorecase, Arg Type : Boolean, Pass By : Value // Return Type : String /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Description: Replace all occurrences of one string inside another with // a new string. // // Arguments: // as_Source The string being searched. // as_Old The old string being replaced. // as_New The new string. // ab_IgnoreCase A boolean stating to ignore case sensitivity. // // Returns: string // as_Source with all occurrences of as_Old replaced with as_New. // If any argument's value is NULL, function returns NULL. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// Long ll_Start Long ll_OldLen Long ll_NewLen String ls_Source //Check parameters If IsNull(as_source) or IsNull(as_old) or IsNull(as_new) or IsNull(ab_ignorecase) Then string ls_null SetNull(ls_null) Return ls_null End If //Get the string lenghts ll_OldLen = Len(as_Old) ll_NewLen = Len(as_New) //Should function respect case. If ab_ignorecase Then as_old = Lower(as_old) ls_source = Lower(as_source) Else ls_source = as_source End If //Search for the first occurrence of as_Old ll_Start = Pos(ls_Source, as_Old) Do While ll_Start > 0 // replace as_Old with as_New as_Source = Replace(as_Source, ll_Start, ll_OldLen, as_New) //Should function respect case. If ab_ignorecase Then ls_source = Lower(as_source) Else ls_source = as_source End If // find the next occurrence of as_Old ll_Start = Pos(ls_Source, as_Old, (ll_Start + ll_NewLen)) Loop Return as_Source |
Good Luck!