FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour STRTRAN case
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
STRTRAN case
Posted: Tue Jan 28, 2020 11:49 AM
Hi,

I have a text with lower and upper case charachters.
I want to to a replace op a text with another text.
The problem is that I don't know this text is upper or lower case.
How can I replace only that text,and leave the other case normal?
If I do
Code (fw): Select all Collapse
result = strtran(upper(text),alltrim(upper(oldtext)),alltrim(upper(newtext)))

than everything is upper-case.
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: STRTRAN case
Posted: Tue Jan 28, 2020 12:10 PM
Code (fw): Select all Collapse
   ? ISUPPER( "Abcde" )    // .T.
   ? ISUPPER( "abcde" )    // .F.


Seealso

ISALPHA(), ISLOWER(), ISDIGIT(), LOWER(), UPPER()

Regards.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM
Re: STRTRAN case
Posted: Tue Jan 28, 2020 12:11 PM

Marc,
can you give us an example?
bye

Marco Boschi
info@marcoboschi.it
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: STRTRAN case
Posted: Tue Jan 28, 2020 12:19 PM
Marco,

In this example I wat to change the text 'MARCTAG001' with 'PI101'
I don't know the case of 'MarcTag001'

Code (fw): Select all Collapse
<property name="PointRefPointName">MarcTag001</property>


After the replace the result should be
Code (fw): Select all Collapse
<property name="PointRefPointName">PI101</property>


BTW : I'ts not always between 'PointRefPointName', so I can't search fo that.
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 195
Joined: Sun Jul 22, 2012 07:01 PM
Re: STRTRAN case
Posted: Tue Jan 28, 2020 05:01 PM

Marc,

I suggest going here and reading about the hb_StrReplace() function. Much more flexible than StrTran.

https://github.com/Petewg/harbour-core/wiki/hb_S

Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: STRTRAN case
Posted: Tue Jan 28, 2020 05:34 PM
Hola,

can be usefull to see this page https://github.com/Petewg/harbour-core/wiki/Regular-Expressions too

saludos

Marcelo
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: STRTRAN case
Posted: Wed Jan 29, 2020 07:09 AM
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: STRTRAN case
Posted: Wed Jan 29, 2020 08:34 PM
Code (fw): Select all Collapse
   local cText    := "This is HIS List"
   local cSearch  := "IS"
   local cReplace := "99"

   FW_At( cSearch, @cText, nil, nil, .f., .f., nil, cReplace, .t. )
   ? cText  // -> Th99 99 H99 L99t"
   
//   Syntax: FW_AT( acSub, @cString, nStart, nEnd, lWholeWord, lSkipQuotes, @cFound, cReplace, lAll ) // FWH1905


Another approach
Code (fw): Select all Collapse
   local cText    := "This is HIS List"
   local cSearch  := "IS"
   local cReplace := "99"
   local nAt

   do while ( nAt := AT( cSearch, UPPER( cText ) ) ) > 0
      cText  := STUFF( cText, nAt, Len( cSearch ), cReplace )
   enddo

   ? cText //-> "Th99 99 H99 L99t"
Regards



G. N. Rao.

Hyderabad, India
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: STRTRAN case
Posted: Wed Jan 29, 2020 08:41 PM

Thank you Rao,

I think I have to upgrade my FW... :oops:

Or I can use you other approach :D :D

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: STRTRAN case
Posted: Wed Jan 29, 2020 08:43 PM

It is good to upgrade but in the same post, I have also given a solution for older versions.

Regards



G. N. Rao.

Hyderabad, India
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: STRTRAN case
Posted: Wed Jan 29, 2020 08:48 PM

Thank you Rao,

I saw it after I posted my replay :D

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Re: STRTRAN case
Posted: Wed Jan 29, 2020 09:34 PM
Hello everyone;

When searching for text, I find that nothing is as good as using regular expressions. hb_RegExAll() --my favorite, will search for a regular expression on any string and return a double dimensioned array with all matches. Once you do know the matches you could potentially use StrTran to replace with desired text, or you could use hb_RegExReplace() to do so from the very beginning.

To build the regular expression you cal always look at any regular expression tutorial and tester online. That's what I do every time I need to build a regular expression for anything.

For example:
Code (fw): Select all Collapse
#define _SEARCH ">[A-Z]*[a-z]*[A-Z]*[a-z]*[0-9]*<"

LOCAL cYourString := '<property name="PointRefPointName">MarcTag001</property>'
LOCAL cReplaceWith := '>PI101<'

HB_RegExReplace( _SEARCH, cYourString, cReplaceWith )


Hope that helps,


Reinaldo.

Continue the discussion