KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Replace string may "lose" characters
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: June 24, 2009

Please note: this behavior no longer occurs as of 4D v11 SQL Release 7 (11.7).

As explained in Tech Tip 75191, the Replace string command has many new behaviors in 4D v11 SQL. Of particular note is that, because of the support of the Unicode standard, Replace string does not take "ignorable" characters into account. Because of this, there are situations where Replace string may appear to "lose" contents in the resulting string. Here is an example:

C_TEXT($source_t;$result_t)
C_LONGINT($sourceLength_l;$resultLength_l;$i)

For ($i;1;9)
   $source_t:=$source_t+Char($i)
End for

$result_t:=Replace string($source_t;Char(Tab );"a")

$sourceLength_l:=Length($source_t)
$resultLength_l:=Length($result_t)

If ($sourceLength_l#$resultLength_l)
   ALERT("Characters were lost!")
Else
   ALERT("Nothing was lost.")
End if


In this example, the alert "Characters were lost!" will be shown because, while the Tab character can be replaced, the other 8 characters in the source string must be ignored and are therefore "thrown out".

There are 2,612 ignorable characters in 4D v11 SQL (as of 11.4). You can use this code to retrieve the code points for all of them:

C_TEXT($source_t;$result_t)
C_LONGINT($sourceLength_l;$resultLength_l;$i)
ARRAY LONGINT($charCodes_al;0)

For ($i;1;65533)
   $source_t:=Char($i)+"b"
   $result_t:=Replace string($source_t;"b";"a")
   $sourceLength_l:=Length($source_t)
   $resultLength_l:=Length($result_t)
   If ($sourceLength_l#$resultLength_l)
      APPEND TO ARRAY($charCodes_al;$i)
   End if
End for


See Also:
Commented by Josh Fletcher on June 24, 2009 at 1:37 PM
Please note that the code is intended to be run in Unicode mode in 4D v11 SQL.