4D v14.3

Character Reference Symbols

Home

 
4D v14.3
Character Reference Symbols

Character Reference Symbols  


 

The character reference symbols: [[...]]

These symbols are used to refer to a single character within a string. This syntax allows you to individually address the characters of a text variable, string variable, or field.

Compatibility note: Starting with 4D v13, you can no longer view the former Mac OS symbols in the Method editor (≤...≥).

If the character reference symbols appear on the left side of the assignment operator (:=), a character is assigned to the referenced position in the string. For example, if vsName is not an empty string, the following line sets the first character of vsName to uppercase:

 If(vsName#"")
    vsName[[1]]:=Uppercase(vsName[[1]])
 End if

Otherwise, if the character reference symbols appear within an expression, they return the character (to which they refer) as a 1-character string. For example:

  ` The following example tests if the last character of vtText is an At sign "@"
 If(vtText#"")
    If(Character code(Substring(vtText;Length(vtText);1))=At sign)
  ` ...
    End if
 End if
 
  ` Using the character reference syntax, you would write in a simpler manner:
 If(vtText#"")
    If(Character code(vtText[[Length(vtText)]])=At sign)
  ` ...
    End if
 End if

When you use the character reference symbols, you must address existing characters in the string in the same way you address existing elements of an array. For example if you address the 20th character of a string variable, this variable MUST contain at least 20 characters.

  • Failing to do so, in interpreted mode, does not cause a syntax error.
  • Failing to do so, in compiled mode (with no options), may lead to memory corruption, if, for instance, you write a character beyond the end of a string or a text.
  • Failing to do so, in compiled mode, causes an error with the option Range Checking On. For example, executing the following code:
  ` Very bad and nasty thing to do, boo!
 vsAnyText:=""
 vsAnyText[[1]]:="A"

will trigger the Runtime Error shown here:

Example  

The following project method capitalizes the first character of each word of the text received as parameter and returns the resulting capitalized text:

  ` Capitalize text project method
  ` Capitalize text ( Text ) -> Text
  ` Capitalize text ( Source text ) -> Capitalized text
 
 $0:=$1
 $vlLen:=Length($0)
 If($vlLen>0)
    $0[[1]]:=Uppercase($0[[1]])
    For($vlChar;1;$vlLen-1)
       If(Position($0[[$vlChar]];" !&()-{}:;<>?/,.=+*")>0)
          $0[[$vlChar+1]]:=Uppercase($0[[$vlChar+1]])
       End if
    End for
 End if

For example, the line:

 ALERT(Capitalize text("hello, my name is jane doe and i'm running for president!"))

displays the alert shown here:

 
PROPERTIES 

Product: 4D
Theme: String

 
SEE ALSO 

ASCII Codes
Char
Character code
Data Types

 
ARTICLE USAGE

4D Language Reference ( 4D v14 R3)
4D Language Reference ( 4D v14 R2)
4D Language Reference ( 4D v13.5)
4D Language Reference ( 4D v14.3)
4D Language Reference ( 4D v14 R4)

Inherited from : Character Reference Symbols ( 4D v12.4)