viernes, noviembre 05, 2004

Función Global Replace

Powerbuilder provee un conjunto completo de funciones para el manejo de Cadenas(String), apesar de esto, no existe una funcion para poder hacer un reemplazo masivo en una cadena, por ejemplo: tengo la cadena "mi nombre es {field}, el nombre {field} proviene del pais ..., saludos {field}" y quiero reemplazar el string {field} con "Carlos Lone". Navegando por el sitio de code-exchange de sybase encontre una función para poder hacer esta acción, el codigo lo pongo a continuación:

/*A String Occurrence Search and Replace RoutineThe following code demonstrates
a string occurrence search and replaceroutine.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".
Parameters
Name = source Type = String
Name = look_for Type = String
Name = replace_with Type = String
*/

//variables
int start_pos=1,len_look_for

len_look_for = len(look_for)
//find the first occurrence of look_for ...
start_pos = Pos(source,look_for,start_pos)
//only enter the loop if you find whats in look_for
DO WHILE start_pos > 0
//replace look_for with replace_with ...
source = Replace(source,start_pos,Len_look_for,replace_with)
//find the next occurrence of
look_forstart_pos = Pos(source,look_for,start_pos+Len(replace_with))
LOOP

return source

Espero que esta función les pueda ser de mucha utilidad.

Saludos,

Ing. Carlos A. Lone
Guatemala

1 Comments:

At 29 de abril de 2009 19:50, Blogger German said...

Carlos q buenisima esa funcion, te lo agradezco, te puedo preguntar como hacer reportes en pb pero que tengan una buena presentacion?

gracias!

 

Publicar un comentario

<< Home