Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replace Function

4,248 views
Skip to first unread message

Filetme

unread,
Aug 12, 2008, 11:59:00 AM8/12/08
to
Is there such thing as a replace function in PowerBuilder?

Let's say I need to check a string, but that string could contain
French characters so I need to remove all French characters and
replace them with the their English counterpart.

Is there a way to do this?

Terry Dykstra

unread,
Aug 12, 2008, 12:03:42 PM8/12/08
to
No, but it's easy to write a function to do it. Here's some code I picked
up somewhere a long time ago. Function takes 3 string arguments:
source, look_for, replace_with

long 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_for
start_pos = Pos(source,look_for,start_pos+Len(replace_with))
LOOP
return source

--
Terry Dykstra (TeamSybase)
http://powerbuilder.codeXchange.sybase.com/
http://casexpress.sybase.com
product enhancement requests:
http://my.isug.com/cgi-bin/1/c/submit_enhancement

"Filetme" <phil_...@hotmail.com> wrote in message
news:2e7d60b2-b0eb-4e52...@x41g2000hsb.googlegroups.com...

Filetme

unread,
Aug 12, 2008, 12:50:19 PM8/12/08
to
On Aug 12, 1:03 pm, "Terry Dykstra" <tddyks...@forestoil.ca> wrote:
> No, but it's easy to write a function to do it.  Here's some code I picked
> up somewhere a long time ago.  Function takes 3 string arguments:
> source, look_for, replace_with
>
> long 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_for
>  start_pos = Pos(source,look_for,start_pos+Len(replace_with))
> LOOP
> return source
>
> --
> Terry Dykstra (TeamSybase)http://powerbuilder.codeXchange.sybase.com/http://casexpress.sybase.com> "Filetme" <phil_la...@hotmail.com> wrote in message

>
> news:2e7d60b2-b0eb-4e52...@x41g2000hsb.googlegroups.com...
>
>
>
> > Is there such thing as a replace function in PowerBuilder?
>
> > Let's say I need to check a string, but that string could contain
> > French characters so I need to remove all French characters and
> > replace them with the their English counterpart.
>
> > Is there a way to do this?- Hide quoted text -
>
> - Show quoted text -

I appreciate the response ...

I saw this code but I'm not sure it will work for me.

I need to do the equivalent of this Oracle command in PB.
translate(upper(ltrim(rtrim(COLUMN_VALUE))),
'1ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜ','1AAAAAACEEEEIIIIOOOOOUUUU')

So I don't really know any positions ... would I just look through the
string as well as the translate list checking for matches?

Real Gagnon

unread,
Aug 12, 2008, 2:15:07 PM8/12/08
to
> Let's say I need to check a string, but that string could contain
> French characters so I need to remove all French characters and
> replace them with the their English counterpart.

String accent = "ИЙКЛЫЩПОАВФийклыщпоавф"
String noaccent = "EEEEUUIIAAOeeeeuuiiaao"

String test = "Test : а й А Й"
String currentChar = ""
String result = ""
int i,j = 0

FOR i = 1 TO len(test)
currentChar = mid(test,i, 1)
j = pos(accent, currentChar)
IF j > 0 THEN
result += mid(noaccent,j,1)
ELSE
result += currentChar
END IF
NEXT

MessageBox(test, result)


Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html

Jeremy Lakeman

unread,
Aug 12, 2008, 8:21:01 PM8/12/08
to
On Aug 13, 3:15 am, Real Gagnon <realgag+use...@geocities.com> wrote:
> > Let's say I need to check a string, but that string could contain
> > French characters so I need to remove all French characters and
> > replace them with the their English counterpart.
>
> String accent   = "ÈÉÊËÛÙÏÎÀÂÔèéêëûùïîàâô"
> String noaccent = "EEEEUUIIAAOeeeeuuiiaao"
>
> String test = "Test : à é À É"

> String currentChar = ""
> String result = ""
> int i,j = 0
>
> FOR i = 1 TO len(test)
>         currentChar = mid(test,i, 1)
>         j = pos(accent, currentChar)
>         IF j > 0 THEN
>                 result += mid(noaccent,j,1)
>         ELSE
>                 result += currentChar
>         END IF
> NEXT
>
> MessageBox(test, result)
>
> Bye.
> --
> Real Gagnon  from  Quebec, Canada
> * Java, Javascript, VBScript and PowerBuilder code snippets
> *http://www.rgagnon.com/howto.html
> *http://www.rgagnon.com/bigindex.html

Ew. If you're going to process a single character at a time, at least
use a character array.
All those +='s are going to seriously slow down this loop for strings
of any significant length.

Real Gagnon

unread,
Aug 13, 2008, 11:00:55 AM8/13/08
to
> Ew. If you're going to process a single character at a time, at least
> use a character array.
> All those +='s are going to seriously slow down this loop for strings
> of any significant length.

I agree. That's exactly how my Java version is designed.

http://www.rgagnon.com/javadetails/java-0456.html

For the array lookup, I was to lazy to do it in PB!

How do you do fast string concatenation in PB ?

But for _simple need_ like name and address processing in _interactive
application_, this solution is good enough.

Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets

Jeremy Lakeman

unread,
Aug 14, 2008, 9:17:44 PM8/14/08
to

How do *I* do it? With a custom class I wrote to replace the built in
blob type.
Well, it still uses a blob internally, but I wrote a faster interface
for most operations. And a heap of useful shorthand methods for
things.

I've posted something like this before, but here's my fast string
building method;

public subroutine of_build_string (readonly string as_value, encoding
an_encoding);
long ll_ret

if isnull(as_value) then return

do
ll_ret = this.blobedit(il_length +1,as_value,an_encoding)
if not isnull(ll_ret) then exit

if len(ib_data)=0 then
ib_data=blob(space(128))
continue
end if

ib_data+=ib_data
loop while true

// drop the null terminator
if an_encoding = encodingansi! then
il_length=ll_ret -2
end subroutine

Jeremy Lakeman

unread,
Aug 14, 2008, 9:18:52 PM8/14/08
to

Bah, hit submit before I finished... that should have ended with;

// drop the null terminator
if an_encoding=encodingansi! then

il_length=ll_ret -1
else
il_length=ll_ret -2
end if

Real Gagnon

unread,
Aug 18, 2008, 10:55:35 AM8/18/08
to
> How do *I* do it? With a custom class I wrote to replace the built in
> blob type.

Ok, I got it.

I have posted a new HowTo on the subject.

http://www.rgagnon.com/pbdetails/pb-0264.html

Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets

0 new messages