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

POSINS()

POSINS()

Insert characters at a certain position within a string

Syntax

       POSINS( <cString>, <cInsert>, [<nPosition>] ) 
                -> cString

Arguments

<cString> Designates the character string into which characters are inserted.

<cInsertstring> Designates the new characters that are inserted into <cString>.

<nPosition> Designates the position where the new characters are inserted within <cString>. The default value inserts the characters in front of the last character.

Returns

POSINS() returns the string with the inserted characters.

Description

You can use POSINS() to insert characters into an existing character string. The <cInsertstring> characters are inserted into the <cString> at the location specified by <nPosition>.

Note

. The resulting character string is longer than the original, so this function cannot be called by reference.

Examples

       .  Insert "123" at position 2:
              ? POSINS("abcdefgh", "123", 2)   // "a123bcdefgh"
           .  Insert "123" at position 8:
              ? POSINS("abcdefgh", "123", 8)   // "abcdefg123h"
           .  Give an erroneous position:
              ? POSINS("abcdefgh", "123", 10)  // "abcdefgh"

Compliance

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

Platforms

All

Files

Source is pos2.c, library is libct.

Seealso

POSCHAR, POSDEL(), POSREPL()

POSDEL()

POSDEL()

Delete characters at a certain position within a string

Syntax

       POSDEL(<cString>,[<nStartPos>],[<nNumber>])
              --> cString

Arguments

<cString> Designates the character string from which the characters are deleted.

<nStartPosition> Designates from which position the deletion begins.

<nNumber> Designates the number of characters to delete.

Returns

The modified string is returned.

Description

This function permits the removal of <nNumber> of characters from <cString>, beginning from <nStartPos>.

Note

. <nStartPos> is optional. If this parameter is not specified, then POSDEL() begins at the end of the <cString> and deletes the specified number (<nNumber>) of characters.

Examples

       .  Delete two characters from a string:
              ? POSDEL("Parameter", 3, 2)         // "Pameter"
           .  Delete the last two characters:
              ? POSDEL("Parameter", , 2)          // "Paramet"

Compliance

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

Platforms

All

Files

Source is pos2.c, library is libct.

Seealso

POSCHAR(), POSINS(), POSREPL()

POSCHAR()

POSCHAR()

Replace character at a certain position within a string

Syntax

       POSCHAR(<cString>,<cCharacter|nCharacter>,
              [<nPosition>]) --> cString

Arguments

<cString> [@] Designates the character string within which the individual character

<cCharacter|nCharacter> is substituted. <cCharacter|nCharacter> Designates an individual character or a numeric ASCII value from 0 to 255.

<nPosition> Designates the position at which the character is substituted. The default value is the last position in <cString>.

Returns

The string returned is the <cString> with a <cCharacter|nCharacter> character in the selected position.

Description

POSCHAR() allows you to replace an individual character within a string without having to split the string. Since this character is only substituted at the designated position within <cString>, the string that is modified can be passed by reference.

Note

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

Examples

       .  Substitute a character at position 3:
              ? POSCHAR("ABCDEF", "X", 3)   // "ABXDEF"
           .  Substitute a character in the last position:
              ? POSCHAR("ABCDEF", "X")      // "ABCDEX"
           .  Pass the string by reference:
              CSETREF(.T.)                  // Suppress return value
              cVar  := "ABCDEF"
              POSCHAR(cVar, "X")
              ? cVar                        // "ABCDEX"

Compliance

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

Platforms

All

Files

Source is pos2.c, library is libct.

Seealso

POSDEL(), POSINS(), POSREPL(), CSETREF()

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()

CharRepl()

CHARREPL()

Replacement of characters

Syntax

      CHARREPL( <cSearchString>, <[@]cString>,
                <cReplaceString>, [<lMode>] ) -> cString

Arguments

<cSearchString> is a string of characters that should be replaced

<[@]cString> is the processed string

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

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

Returns

<cString> the processed string

Description

The CHARREPL() function replaces certain characters in <cString> with others depending on the setting of <lMode>. If <lMode> is set to .F., the function takes the characters of <cSearchString> one after the other, searches for them in <cString> and, if successful, replaces them with the corresponding character of <cReplaceString>. Be aware that if the same characters occur in both <cSearchString> and <cReplaceString>, the character on a certain position in <cString> can be replaced multiple times. if <lMode> is set to .T., the function takes the characters in <cString> one after the other, searches for them in <cSearchString> and, if successful, replaces them with the corresponding character of <cReplaceString>. Note that no multiple replacements are possible in this mode. If <cReplaceString> is shorter than <cSearchString>, the last character of <cReplaceString> is used as corresponding character for the the “rest” of <cSearchString>. One can omit the return value by setting the CSETREF() switch to .T., but then one must pass <cString> by reference to get the result.

Examples

      ? charrepl( "1234", "1x2y3z", "abcd" )            // "axbycz"
      ? charrepl( "abcdefghij", "jhfdb", "1234567890" ) // "08642"
      ? charrepl( "abcdefghij", "jhfdb", "12345" )      // "55542"
      ? charrepl( "1234", "1234", "234A" )              // "AAAA"
      ? charrepl( "1234", "1234", "234A", .T. )         // "234A"

Tests

      charrepl( "1234", "1x2y3z", "abcd" ) == "axbycz"
      charrepl( "abcdefghij", "jhfdb", "1234567890" ) == "08642"
      charrepl( "abcdefghij", "jhfdb", "12345" ) == "55542"
      charrepl( "1234", "1234", "234A" ) == "AAAA"
      charrepl( "1234", "1234", "234A", .T. ) == "234A"

Compliance

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

Platforms

All

Files

Source is charrepl.c, library is ct3.

Seealso

WORDREPL(), POSREPL(), RANGEREPL(), CSETREF()