Substituindo parte do texto em uma string

Substituindo parte do texto em uma string

Overview

Quem trabalha com PowerBuilder sabe o desafio que é manipular textos de forma eficiente. Se você já se viu repetindo ações para substituir um texto por outro, tenho boas notícias. Hoje, vamos mergulhar no mundo da função of_GlobalReplace, um tesouro escondido no PowerBuilder que promete simplificar sua vida. Vamos desvendar como essa função, direto do arsenal do PFC, pode substituir todas as ocorrências de um texto específico, com direito a tratar erros e ignorar maiúsculas e minúsculas. Acompanhe!

A função Replace do PowerBuilder substitui apenas um pedaço do texto, mas não substitui todas as ocorrências do texto A no no texto B. O fonte abaixo mostra como fazer isso.

Como não é bom reinventar a roda, o fonte abaixo foi extraído do PFC.

 1//////////////////////////////////////////////////////////////////////////////
 2//
 3//	Function:  		of_GlobalReplace
 4//
 5//	Access:  		public
 6//
 7//	Arguments:
 8//	as_Source		The string being searched.
 9//	as_Old			The old string being replaced.
10//	as_New			The new string.
11// ab_IgnoreCase	A boolean stating to ignore case sensitivity.
12//
13//	Returns:  		string
14//						as_Source with all occurrences of as_Old replaced with as_New.
15//						If any argument's value is NULL, function returns NULL.
16//
17//	Description:  	Replace all occurrences of one string inside another with
18//						a new string.
19//
20//////////////////////////////////////////////////////////////////////////////
21//
22//	Revision History
23//
24//	Version
25//	5.0   Initial version
26//
27//////////////////////////////////////////////////////////////////////////////
28//
29/*
30 * Open Source PowerBuilder Foundation Class Libraries
31 *
32 * Copyright (c) 2004-2005, All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted in accordance with the GNU Lesser General
36 * Public License Version 2.1, February 1999
37 *
38 * http://www.gnu.org/copyleft/lesser.html
39 *
40 * ====================================================================
41 *
42 * This software consists of voluntary contributions made by many
43 * individuals and was originally based on software copyright (c) 
44 * 1996-2004 Sybase, Inc. http://www.sybase.com.  For more
45 * information on the Open Source PowerBuilder Foundation Class
46 * Libraries see http://pfc.codexchange.sybase.com
47*/
48//
49//////////////////////////////////////////////////////////////////////////////
50
51Long	ll_Start
52Long	ll_OldLen
53Long	ll_NewLen
54String ls_Source
55
56//Check parameters
57If IsNull(as_source) or IsNull(as_old) or IsNull(as_new) or IsNull(ab_ignorecase) Then
58	string ls_null
59	SetNull(ls_null)
60	Return ls_null
61End If
62
63//Get the string lenghts
64ll_OldLen = Len(as_Old)
65ll_NewLen = Len(as_New)
66
67//Should function respect case.
68If ab_ignorecase Then
69	as_old = Lower(as_old)
70	ls_source = Lower(as_source)
71Else
72	ls_source = as_source
73End If
74
75//Search for the first occurrence of as_Old
76ll_Start = Pos(ls_Source, as_Old)
77
78Do While ll_Start > 0
79	// replace as_Old with as_New
80	as_Source = Replace(as_Source, ll_Start, ll_OldLen, as_New)
81	
82	//Should function respect case.
83	If ab_ignorecase Then 
84		ls_source = Lower(as_source)
85	Else
86		ls_source = as_source
87	End If
88	
89	// find the next occurrence of as_Old
90	ll_Start = Pos(ls_Source, as_Old, (ll_Start + ll_NewLen))
91Loop
92
93Return as_Source

Ela é um pouco grande, mas realiza tratamentos de erros, além de realizar a substituição de todas as ocorrências do texto A no texto B.