WordToChar()

WordToChar()

Replace double with single characters

Syntax

       WordToChar( <cDoubleCharacterSearchString>, <cString>,
                   <cSingleCharacterReplaceString> ) -> cString

Arguments

<cDoubleCharacterSearchString> Designates multiple 2-byte sequences which are exchanged for corresponding individual characters from the <cSingleCharacter ReplaceString>.

<cString> Designates the character string within which 2-byte sequences are exchanged for individual characters.

<cSingleCharacterReplaceString> Designates a sequence of individual characters which correspond to 2-byte sequences in <cDoubleCharacterSearchString> for replacement within <cString>.

Returns

WordToChar() returns the modified character string.

Description

When you use SOUNDEX algorithms, sequences of two characters must often be exchanged for a single other character. WordToChar() makes this process extremely simple and quick. The function processes the <cString> in 1-byte steps. The behavior after exchanging a sequence for a character is determined by CSetAtMuPa(). If CSetAtMuPa is .F., the search for more sequences continues after the exchanged characters. If CSetAtMuPa is .T., the search for more sequences continues and includes the exchanged characters.

Notes

. The term “word” is not used here in the textual sense, but rather as it is used in assembler programming. A “word” consists of units of 16 bits, or more precisely, 2 bytes. . <cCharacterlist> can be shorter than <cDoubleCharacterList>. When this occurs, the function exchanges the sequences in <cDoubleCharacterList> that do not have corresponding sequences in <cCharacterlist> for the last sequence in <cDoubleCharacterList>.

Examples

       .  This example shows a simple replacement:
              ? WordToChar("aa", "Xaaaa", "a")      // "Xaa"
       .  This example shows WORDTOCHAR() in conjunction with
          CSetAtMuPa():
          CSetAtMuPa(.F.)                       // Multi pass off
          ? WordToChar("aa", "Xaaaa", "a")      // "Xaa"
          CSetAtMuPa(.T.)                       // Multi pass on
          ? WordToChar("aa", "Xaaaa", "a")      // "Xa"

Compliance

WordToChar() is compatible with CT3’s WordToChar().

Platforms

All

Files

Source is wordtoch.c, library is libct.

Seealso

CSetAtMuPa(), CharRepl(), WordRepl()

RANGEREPL()

RANGEREPL()

Replace characters within a certain ASCII range from a string

Syntax

       RANGEREPL( <cChar1|nChar1>, <cChar2|nChar2>,
           <[@]cString>, <cReplacementChar|nReplacementChar> ) 
           -> cString

Arguments

<cChar1|nChar1> and <cChar2|nChar> Designate the first and the last character of the character range.

<cString> [@] Designates the string that is processed.

<cReplacementChar|nReplacementChar> Designates the single character that replaces those characters in <cString> that are within the specified range.

Returns

The processed character string is returned.

Description

All characters that are in a particular range can be replaced by a new character. For example, you could replace all control characters with spaces. The designated range can also run from “back to front”, meaning that <cChar2|nChar2 can have a lower value than <cChar1|nChar1. In such a case the range extends from the larger value to 255 and from 0 to the smaller value.

Notes

. The length of <cString> remains unaffected by this function. . The return value of this function can be suppressed by implementing CSETREF() to save room in working memory.

Examples

        . ? rangerepl( "0", "9", "year2002.dbf", "?" )
                              // "year????.dbf", replace all digits
        . ? rangerepl( "9", "0", "year2002.dbf", "?" )
                  // "????2??2????", testing replacement from
                  // "9" to chr(255) and from chr(0) to "0"
        . ? rangerepl( "0", "9", "yearcurr.dbf", "?" )
                  // "yearcurr.dbf", test leaving string untouched
        .  Exchange all control characters in a character string for the
              character ".":
              cString  :=  "a" + CHR(5) + "b" + CHR(9)
              ? RANGEREPL(CHR(0), CHR(31), cString, ".")   // "a.b."
        .  A null string can be specified, instead of CHR(0).  The
           following example exchanges all characters with a code < "A" for
           the number "0".  Of course, the number "0" remains the number "0":
           ? RANGEREPL("", CHR(65), "123400", "0")      // "000000"
        .  All characters in the range "0" to "8" are exchanged for the
           number "9":
           ? RANGEREPL("0", "8", "0212 - 78 67 43", "9")
                                                        // "9999 - 99 99 99"
        .  With the exception of upper case letters, all characters are
           exchanged for dashes.  The optimum call is in conjunction with
           CSETREF():
           CSETREF(.T.)
           cString  :=  "A()&BC/?D"
           RANGEREPL(91, 64, @cString, "-")             // "A--BC--D"

Tests

       rangerepl( "0", "9", "year2002.dbf", "?" ) == "year????.dbf"
       rangerepl( "9", "0", "year2002.dbf", "?" ) == "????2??2????"
       rangerepl( "0", "9", "yearcurr.dbf", "?" ) == "yearcurr.dbf"

Compliance

RANGEREPL() is compatible with CT3’s RANGEREPL().

Platforms

All

Files

Source is range.c, library is libct.

Seealso

RANGEREM(), POSREPL(), CHARREPL(), CSETREF(), Introduction

WordRepl()

WordRepl()

Replacement of double characters

Syntax

      WordRepl( <cDoubleCharacterSearchString>, <[@]cString>,
                <cDoubleCharacterReplaceString>, [<lMode>] ) -> <cString>

Arguments

<cDoubleCharacterSearchString> is a string of double characters that should be replaced

<[@]cString>is the processed string

<cDoubleCharacterReplaceString> is a string of double characters that replace the one of <cSearchString>

[<lMode>] sets the replacement method (see description) Default: .F.

Returns

<cString> the processed string

Description

The WordRepl() takes the double characters of <cDoubleCharacterSearchString> one after the other and searches for them in <cString>. For <lMode> set to .F., this search is successful, if the double character sequence in <cString> starts at an odd position or at any position, if <lMode> is set to .T.

If this happens, the double character sequence will be replaced with the corresponding double character sequence of <cDoubleCharacter ReplaceString>. If <cDoubleCharacter ReplaceString> is shorter than <cDoubleCharacter SearchString> the last double sequence of <cDoubleCharacter ReplaceString> is used for the “rest” of <cDoubleCharacter SearchString>. Note that the last double character sequence in “AABBC” is “BB” in this context !!

After the replacement the function restarts the search in <cString> BEHIND the replacement if the CSETATMUPA() switch is turned off, or BEHIND the first character of the replacement if the switch is turned on. (see examples for this !) One can omit the return value of this function by setting the CSETREF() to .T., but one must then pass <cString> by reference to get a result.

Examples

      ? WordRepl( "CC", "AABBCCDDEE", "XX" ) // "AABBXXDDEE"
      ? WordRepl( "aa", "1aaaa", "ba" )      // "1abaa"
      ? WordRepl( "aa", "1aaaa", "ba", .T. ) // "1baba"
      csetatmupa( .T. )
      ? WordRepl( "aa", "1aaaa", "ba" )      // "1abaa"
      ? WordRepl( "aa", "1aaaa", "ba", .T. ) // "1bbba"

Tests

      WordRepl( "CC", "AABBCCDDEE", "XX" ) == "AABBXXDDEE"
      WordRepl( "aa", "1aaaa", "ba" )      == "1abaa"
      WordRepl( "aa", "1aaaa", "ba", .T. ) == "1baba"
      Eval( {|| csetatmupa( .T. ), WordRepl( "aa", "1aaaa", "ba" ) } ) == "1abaa"
      Eval( {|| csetatmupa( .T. ), WordRepl( "aa", "1aaaa", "ba", .T. ) } ) == "1bbba"

Compliance

WordRepl() is compatible with CT3’s WordRepl().

Platforms

All

Files

Source is wordrepl.c, library is ct3.

Seealso

CHARREPL(), RANGEREPL(), POSREPL(), CSETREF(), CSETATMUPA()

CSetRef()

CSetRef()

Determine return value of reference sensitive CT3 string functions

Syntax

      CSetRef( [<lNewSwitch>] ) -> lOldSwitch

Arguments

[<lNewSwitch>] .T. -> suppress return value .F. -> do not suppress return value

Returns

lOldSwitch old (if lNewSwitch is a logical value) or current state of the switch

Description

Within the CT3 functions, the following functions do not change the length of a string passed as parameter while transforming this string:

ADDASCII() BLANK() CHARADD() CHARAND() CHARMIRR() CHARNOT() CHAROR() CHARRELREP() CHARREPL() CHARSORT() CHARSWAP() CHARXOR() CRYPT() JUSTLEFT() JUSTRIGHT() POSCHAR() POSREPL() RANGEREPL() REPLALL() REPLLEFT() REPLRIGHT() TOKENLOWER() TOKENUPPER() WORDREPL() WORDSWAP()

Thus, these functions allow to pass the string by reference [@] to the function so that it may not be necessary to return the transformed string. By calling CSetRef (.T.), the above mentioned functions return the value .F. instead of the transformed string if the string is passed by reference to the function. The switch is turned off (.F.) by default.

Compliance

This function is fully CT3 compatible.

Platforms

All

Files

Source is ctstr.c, library is ct3.

Seealso

ADDASCII(), BLANK(), CHARADD(), CHARAND(), CHARMIRR(), CHARNOT(), CHAROR(), CHARRELREP(), CHARREPL(), CHARSORT(), CHARSWAP(), CHARXOR(), CRYPT(), JUSTLEFT(), JUSTRIGHT(), POSCHAR(), POSREPL(), RANGEREPL(), REPLALL(), REPLLEFT(), REPLRIGHT(), TOKENLOWER(), TOKENUPPER(), WORDREPL(), WORDSWAP()

Harbour All Functions – C

CD / CHDIR / DirChange

CDoW

Chr

CharAdd
CharAnd
CharEven
CharHist
CharList
CharMirr
CharMix
CharNoList
CharNot
CharOdd
CharOne
CharOnly
CharOr
CharPix
CharRela
CharRelRep
CharRem
CharRepl
CharRLL
CharRLR
CharSHL
CharSHR
CharSList
CharSort
CharSub
CharSwap
CharWin
CharXOR

CLIPINIT
CMonth

Col

Cos

CosH

CountLeft
CountRight

CToBit

CToD

CToDoW
CToF
CToMonth
CToN

CurDir

CSetArgErr
CSetAtMuPa
CSetRef
CTCExit
CTCInit
CTExit
CTInit

CT_CHARREPL

 CHARREPL()
 Replaces certain characters with others
------------------------------------------------------------------------------
 Syntax

     CHARREPL(<cSearchFor>,<cString>,   <cReplaceExpression>,
        [<lMode>]) --> cString

 Arguments

     <cSearchFor>  Designates a list of characters to search for in
     <cString>.

     <cString>  [@]  Designates the character string within which to
     search for and replace the characters in <cSearchFor>.

     <cReplaceExpression>  Designates the character list that replaces
     characters in <cString>.

     <lMode>  Designates whether multiple replacements are made.

     <lMode>  Designates one pass of the <cString> (.T.) or multiple
     replacements (.F.).  The default value is for .F..

 Returns

     The processed <cString> is returned.

 Description

     This function allows you to carry out very complex replacement
     procedures.  Every character in the <cSearchFor> is searched for within
     the <cString>.  If found, the character is replaced by the corresponding
     character in the <cReplaceExpression>.

     If <lMode> is not passed, characters are exchanged repeatedly as
     required.  This means that the function goes through each individual
     character in the found characters in sequence, and then searches the
     entire <cString>, exchanging <cSearchFor> for corresponding characters
     in <cReplaceExpression>.  When you use this technique, characters that
     have already been replaced are exchanged again if the replacement
     character also appears in the search list.

     However, if the optional <lMode> parameter is specified, the function
     proceeds differently.  It goes through each character in <cString> in
     sequence, determining whether or not it should be replaced.  Characters
     that have been replaced are not replaced again.

     As a rule, if the same characters appear within <cReplaceExpression> and
     <cSearchFor>, you must check very closely to determine which <lMode>
     parameter should be used (see example).

 Notes

     .  If the <cReplaceExpression> sequence is shorter than
        <cSearchFor>, the characters that do not have a corresponding
        replacement in <cReplaceExpression> are replaced with the last
        character of <cReplaceExpression> (see example).

     .  The return value of this function can be suppressed by
        implementing CSETREF() to save space in working memory.

 Examples

     .  The number "1" is replaced with the letter "a", the number "2"
        with the letter "b", etc..  If the number "4" appeared in the
        character string, it would be replaced with a "d":

        ? CHARREPL("1234", "1x2y3z", "abcd")              // "axbycz"

     .  The letters a-j are replaced with the numbers 0-9; for
        example, an "f" is replaced with a "6":

        ? CHARREPL("abcdefghij", "jhfdb", "1234567890")   // "08642"

     .  The third parameter makes fewer characters available for the
        exchange.  Therefore the letters f-j are replaced with the last
        characters from "12345":

        ? CHARREPL("abcdefghij", "jhfdb", "12345")        // "55542"

     .  Here is an example of the difference between a specified
        <lMode> parameter (.T.) and the default parameter (.F.):

        ? CHARREPL("1234", "1234", "234A")                // "AAAA"
        ? CHARREPL("1234", "1234", "234A", .T.)           // "234A"

See Also: WORDREPL() WORDTOCHAR() POSREPL() RANGEREPL()

 

Tools – String Manipulations

Introduction 
ADDASCII()   Adds a value to each ASCII code in a string
AFTERATNUM() Returns remainder of a string after nth appearance of sequence
ASCIISUM()   Finds sum of the ASCII values of all the characters of a string
ASCPOS()     Determines ASCII value of a character at a position in a string
ATADJUST()   Adjusts the beginning position of a sequence within a string
ATNUM()      Determines the starting position of a sequence within a string
ATREPL()     Searches for a sequence within a string and replaces it
ATTOKEN()    Finds the position of a token within a string
BEFORATNUM() Returns string segment before the nth occurrence of a sequence
CENTER()     Centers a string using pad characters
CHARADD()    Adds the corresponding ASCII codes of two strings
CHARAND()    Links corresponding ASCII codes of paired strings with AND
CHAREVEN()   Returns characters in the even positions of a string
CHARLIST()   Lists each character in a string
CHARMIRR()   Mirrors characters within a string
CHARMIX()    Mixes two strings together
CHARNOLIST() Lists the characters that do not appear in a string
CHARNOT()    Complements each character in a string
CHARODD()    Returns characters in the odd positions of a string
CHARONE()    Reduces adjoining duplicate characters in string to 1 character
CHARONLY()   Determines the common denominator between two strings
CHAROR()     Joins the corresponding ASCII code of paired strings with OR
CHARPACK()   Compresses (packs) a string
CHARRELA()   Correlates the character positions in paired strings
CHARRELREP() Replaces characters in a string depending on their correlation
CHARREM()    Removes particular characters from a string
CHARREPL()   Replaces certain characters with others
CHARSORT()   Sorts sequences within a string
CHARSPREAD() Expands a string at the tokens
CHARSWAP()   Exchanges all adjoining characters in a string
CHARUNPACK() Decompresses (unpacks) a string
CHARXOR()    Joins ASCII codes of paired strings with exclusive OR operation
CHECKSUM()   Calculates the checksum for a character string (algorithm)
COUNTLEFT()  Counts a particular character at the beginning of a string
COUNTRIGHT() Counts a particular character at the end of a string
CRYPT()      Encrypts and decrypts a string
CSETATMUPA() Determines setting of the multi-pass mode for ATXXX() functions
CSETREF()    Determines whether reference sensitive functions return a value
EXPAND()     Expands a string by inserting characters
JUSTLEFT()   Moves characters from the beginning to the end of a string
JUSTRIGHT()  Moves characters from the end of a string to the beginning
LIKE()       Compares character strings using wildcard characters
LTOC()       Converts a logical value into a character
MAXLINE()    Finds the longest line within a string
NUMAT()      Counts the number of occurrences of a sequence within a string
NUMLINE()    Determines the number of lines required for string output
NUMTOKEN()   Determines the number of tokens in a string
PADLEFT()    Pads a string on the left to a particular length
PADRIGHT()   Pads a string on the right to a particular length
POSALPHA()   Determines position of first alphabetic character in a string
POSCHAR()    Replaces individual character at particular position in string
POSDEL()     Deletes characters at a particular position in a string
POSDIFF()    Finds the first position from which two strings differ
POSEQUAL()   Finds the first position at which two strings are the same
POSINS()     Inserts characters at a particular position within a string
POSLOWER()   Finds the position of the first lower case alphabetic character
POSRANGE()   Determines position of first character in an ASCII code range
POSREPL()    Replaces one or more characters from a certain position
POSUPPER()   Finds the position of the first uppercase, alphabetic character
RANGEREM()   Deletes characters that are within a specified ASCII code range
RANGEREPL()  Replaces characters within a specified ASCII code range
REMALL()     Removes characters from the beginning and end of a string
REMLEFT()    Removes particular characters from the beginning of a string
REMRIGHT()   Removes particular characters at the end of a string
REPLALL()    Exchanges characters at the beginning and end of a string
REPLLEFT()   Exchanges particular characters at the beginning of a string
REPLRIGHT()  Exchanges particular characters at the end of a string
RESTTOKEN()  Recreates an incremental tokenizer environment
SAVETOKEN()  Saves the incremental tokenizer environment to a variable
SETATLIKE()  Provides an additional search mode for all AT functions
STRDIFF()    Finds similarity between two strings (Levenshtein Distance)
STRSWAP()    Interchanges two strings
TABEXPAND()  Converts tabs to spaces
TABPACK()    Converts spaces in tabs
TOKEN()      Selects the nth token from a string
TOKENAT()    Determines the most recent TOKENNEXT() position within a string
TOKENEND()   Determines if more tokens are available in TOKENNEXT()
TOKENINIT()  Initializes a string for TOKENNEXT()
TOKENLOWER() Converts initial alphabetic character of a token into lowercase
TOKENNEXT()  Provides an incremental tokenizer
TOKENSEP()   Provides separator before/after most recently retrieved TOKEN()
TOKENUPPER() Converts the initial letter of a token into upper case
VALPOS()     Determines numerical value of character at particular position
WORDONE()    Reduces multiple appearances of double characters to one
WORDONLY()   Finds common denominator of 2 strings on double character basis
WORDREPL()   Replaces particular double characters with others
WORDSWAP()   Exchanges double characters lying beside each other in a string
WORDTOCHAR() Exchanges double characters for individual ones