Paradox Community

Items in pnews.paradox-programming

Subject:Re: Quick and Dirty Search and Replace
Date:Sat, 5 May 2018 14:42:18 +1000
From:Robert MacMillan <macfam@bigpond.net.au>
Newsgroups:pnews.paradox-programming
Thanks. Exactly what I was looking for.

On 4/05/2018 11:29 PM, Mark Bannister wrote:
>
> The code below is from the strings library made by Robert Wiltshire and
> others in the community.  I've attached the entire library but this is
> the copy I use in my application so there is code here that you will
> have to remove in order for this to work. (I think the code you need to
> remove is only a Uses call to a library of custom data types.)  There
> are also duplicate methods as I merged Robert's final library with my
> own code and didn't bother to clean it up.
> The methods that are nicely documented are from Robert.  The crappy ones
> are mine.
>
>
>
> method removeChars( stOrig string,
>                             stCharsToRemove string ) string
>
> ; Purpose : Remove any of the chars in a string from another string
> ;
> ; Parameters:
> ;      stOrig : original string we are searching through
> ;      stCharsToRemove : string containing chars we want to remove
> ;
> ; Return: string with all of chars removed
> ;
> ; Tests
> ;    #1 stOrig must not be blank
> ;    #2 stCharsToRemove must not be blank
> ;
> ; Version 1.00  02/21/2005  Submitted by R Wiltshire
> ; Version 1.01  4/1/2005 Changes R Wiltshire
> ;     added check for ignoreCase, breakapart always looks at case
> ;      so additional work needs to be done to truly ignore case in this
> method
> ;     not sure if that will effect non-english users, need feedback please
>
> var
>  arPieces arStringType
>  stRemove string
> endvar
>
> ; Test #1 for blank string being passed
> if stOrig = "" then
>    return("")
>   endif
>
> ; Test#2  if nothing to remove, then return original string
> if stCharsToRemove = "" then
>    return(stOrig)
>   endif
>
> ; added this 4/1/2005  breakapart is not case sensitive
> if isIgnoreCaseInStringCompares()
>    then
>         ; to ignore case, we need to get R if user specified r, etc...
>         stRemove = stCharsToRemove + getStringOppositeCase(stCharsToRemove)
>    else
>         ; not ignoring case, simply take out what user passed
>         stRemove = stCharsToRemove
>    endif
>
> ; easy call to breakapart to eliminate these characters
> ; need to call this with stRemove not stCharsToRemove  4/1/2005 rw
> stOrig.breakApart(arPieces,stRemove)
>
> if arPieces.size() = 0 then
>    return("")
>   endif
>
> if arPieces.size() = 1 then
>    return(arPieces[1])
>   endif
>
> ; now stick all pieces together
> return(concatString_Array(arPieces))
>
> endMethod
>
> method concatString_array( ar arStringType) string
>
> ; Purpose : fast string concatenation of an array of strings
> ;
> ; Parameters:
> ;    ar  : array of strings that we need to concatenate
> ;
> ; Return Value:
> ;    a string that is the result of the concatenation
> ;
> ; Test: N/A
> ;
> ; Version 1.00 4/20/2005  From Internet Newsgroup - Kudos to UWE
> ;       tested,timed, and typed into library by R Wiltshire
>
>
> ; test for special case of empty array
> if ar.size()=0 then
>    return("")
>   endif
>
> return(concatRecur(ar,1,ar.size()))
>
> endMethod
>
>
> method concatRecur( ar arStringType, i LongInt, j LongInt ) String
>
> ; Purpose : fast string concatenation of an array of strings
> ;   this is core call for concatString_array
> ;   this method calls itself recursively
> ;    very fast algorithm....
> ;
> ; Parameters:
> ;    ar  : array of strings that we need to concatenate
> ;    i   : first item in array
> ;    j   : last item in array
> ;
> ; Return Value:
> ;    a string that is the result of the concatenation
> ;
> ; Test: N/A
> ;
> ; Version 1.00 4/20/2005  From Internet Newsgroup - Kudos to UWE
> ;       tested,timed, and typed into library by R Wiltshire
>
>
> return iif(i=j, ar[i], concatRecur(ar,i,(i+j)/2) +
>            concatRecur(ar,(i+j)/2+1,j) )
>
> endMethod
>
>
>
> On 5/4/2018 5:30 AM, Robert MacMillan wrote:
>> Over the years there have been various bits of code offered for this
>> and apparently Rick Kelly had something but that is no longer available.
>>
>> So my interest in this is getting rid of the "'" in Horse Names or
>> anything else that causes problems like "(NZ)". So names that can be
>> "She's Beautiful" or "Mr'Oleary" or Look'n'Good. as examples.
>>
>> So the quick and dirty code that I am currently using is:-
>>
>> if tc.Name.search("'")>0
>>             Then
>>              tc.Name.Breakapart(atStr, "'")
>>              s=""
>>              For k from 1 to atStr.size()
>>                     s=s+atStr[k]
>>              Endfor
>>                tc.HName=s
>>           Else tc.HName=tc.Name
>> Endif
>>
>> Obviously this works absolutely perfectly except for two cases and
>> that is where the Breakapart String being searched for is the entire
>> end of the string or the entire beginning of the string. The extension
>> is it can also be used as a "Search and Replace" by forming the new
>> string by, lets assume that one wants the "'" to be replaced by "you
>> are beautiful". If the substring is at the beginning I can add a test
>> for the position of the first instance of the substring also being the
>> first character of the string - not demonstrated here because it never
>> occurs in horse names.
>>
>> s=""
>> For k from 1 to atStr.size()
>>                     s=s+"you are beautiful"+atStr[k]
>> Endfor
>>
>> So now turning it over to the brains trust. Where I have got to so far
>> is very fast and very efficient although rather less elegant than
>> other solutions that have been posted previously. I can see how to
>> deal with the first part of the entire string being identifiable but I
>> cannot see how to overcome the situation where the characters in a
>> string at the end are those that we want to replace and there are
>> other instances within the string because the array will not identify
>> that there is another replacement required.
>>
>> I know this looks very simple compared to the other solutions that
>> have been offered but it works and it is simple to code and is very fast.
>>
>> Obviously one can code passing several different strings being
>> searched and replaced and dealing with them sequentially but I still
>> can not see how to deal with the case of one of the strings to be
>> replaced being at the end of the string being searched.
>>
>> Need help from the brains trust but for what I want where I want to
>> get rid of for example "(NZ)" this works well.
>>
>>
>>
>


Copyright © 2004 thedbcommunity.com