Page 1 of 1
STRTRAN case
Posted: Tue Jan 28, 2020 11:49 am
by Marc Vanzegbroeck
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: Select all
result = strtran(upper(text),alltrim(upper(oldtext)),alltrim(upper(newtext)))
than everything is upper-case.
Re: STRTRAN case
Posted: Tue Jan 28, 2020 12:10 pm
by karinha
Code: Select all
? ISUPPER( "Abcde" ) // .T.
? ISUPPER( "abcde" ) // .F.
Seealso
ISALPHA(), ISLOWER(), ISDIGIT(), LOWER(), UPPER()
Regards.
Re: STRTRAN case
Posted: Tue Jan 28, 2020 12:11 pm
by MarcoBoschi
Marc,
can you give us an example?
bye
Re: STRTRAN case
Posted: Tue Jan 28, 2020 12:19 pm
by Marc Vanzegbroeck
Marco,
In this example I wat to change the text 'MARCTAG001' with 'PI101'
I don't know the case of 'MarcTag001'
Code: Select all
<property name="PointRefPointName">MarcTag001</property>
After the replace the result should be
Code: Select all
<property name="PointRefPointName">PI101</property>
BTW : I'ts not always between 'PointRefPointName', so I can't search fo that.
Re: STRTRAN case
Posted: Tue Jan 28, 2020 5:01 pm
by rhlawek
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
Re: STRTRAN case
Posted: Tue Jan 28, 2020 5:34 pm
by Marcelo Via Giglio
Hola,
can be usefull to see this page
https://github.com/Petewg/harbour-core/ ... xpressions too
saludos
Marcelo
Re: STRTRAN case
Posted: Wed Jan 29, 2020 7:09 am
by Marc Vanzegbroeck
Re: STRTRAN case
Posted: Wed Jan 29, 2020 8:34 pm
by nageswaragunupudi
Code: Select all
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: Select all
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"
Re: STRTRAN case
Posted: Wed Jan 29, 2020 8:41 pm
by Marc Vanzegbroeck
Thank you Rao,
I think I have to upgrade my FW...
Or I can use you other approach
Re: STRTRAN case
Posted: Wed Jan 29, 2020 8:43 pm
by nageswaragunupudi
It is good to upgrade but in the same post, I have also given a solution for older versions.
Re: STRTRAN case
Posted: Wed Jan 29, 2020 8:48 pm
by Marc Vanzegbroeck
Thank you Rao,
I saw it after I posted my replay
Re: STRTRAN case
Posted: Wed Jan 29, 2020 9:34 pm
by reinaldocrespo
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: Select all
#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.