String

From Team Developer SqlWindows Wiki
Revision as of 12:55, 12 September 2008 by 62.58.16.59 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page covers string datatype tips & tricks.

Contents


Pointer.pngHow to put trailing spaces in a string

If you want to append spaces to a string up until a certain length, use this function

   sResult = VisStrPad( sSource, nResultStringSize )

You can find this function in vtstr.apl

Example : When you have a string with 3 characters and want to add trailing spaces up until the result string is 5 characters

   Set sSource = "ABC"      ! Source has 3 characters
   Set sResult = VisStrPad( sSource, 5 )

   ! sResult has the value "ABC  "

When the source string is greater than the requested size, the source string will be truncated.
This means the result string will allways be the same size as the nResultStringSize parameter.


Pointer.pngHow to put prefix characters in a string

If you want to place prefix characters to a string use this function.

Function: PALStrPrefix
   Returns
      String:
   Parameters
      String: psToPrefix
      String: psPrefixCharacter
      Number: pnTotalLength
   Local variables
      Number: nFieldLength
   Actions
      Set nFieldLength = SalStrLength( psToPrefix )
      If nFieldLength < pnTotalLength
         Return SalStrRepeatX( SalStrLeftX( psPrefixCharacter, 1 ), pnTotalLength - nFieldLength ) || psToPrefix
      Else If nFieldLength = pnTotalLength
         Return psToPrefix
      Else
         Return SalStrLeftX( psToPrefix, pnTotalLength )

Example : to put an X character as prefix to a string with 3 characters up until it has a resulting length of 5 characters

   Set sSource = "ABC"      ! Source has 3 characters
   Set sResult = PALStrPrefix( sSource, "X", 5 )

   ! sResult has the value "XXABC"


Pointer.pngAn alternative way to define a string in boolean expressions

Look at the next piece of code

   If bOk = TRUE
      Set sValue = "It seems all is OK"
   Else
      Set sValue = "An error"

It can be rewritten using one line of code using this function from vtstr.apl

   sResult = VisStrChoose( bExpression, sTrueString, sFalseString )

So the If/Else construction above can be rewritten to

   Set sValue = VisStrChoose( bOk = TRUE, "It seems all is OK", "An error" )


See also Number : An alternative way to define a number in boolean expressions



Pointer.pngHow to define and use a newline in strings

A nice way to define a newline is to create a string constant in your source.

String: NEWLINE = '
'

When entering this constant, press CTRL-ENTER after the first apostrofe.

Here a few samples using this constant.

   Set sText = "This is the first line" || NEWLINE || "This is the second line" || NEWLINE || "This is the third line"
   If SalStrScan( sText, NEWLINE ) > -1
      ! The string contains newlines, change the newlines into comma's
      Set sResult = VisStrSubstitute( sText, NEWLINE, ",")


Pointer.pngNewline representation

A newline in a string is represented as two characters, the CR (Carriage Return) followed by LF (Line Feed).
The CR character has ASCII value 0x0D
The LR character has ASCII value 0x0A

To construct a newline from the ASCII values

   sNewline = SalNumberToChar (0x0D) || SalNumberToChar (0x0A)

This is the same as the next assignment

   sNewline = '
'

Pointer.pngHigh performance string fills

See WinApi_Miscellaneous : How to fill strings with a specific character