Working With String Data in Recital

From Recital Documentation Wiki
Jump to: navigation, search

Working With String Data in Recital

Changing the Case of a String

  • lower() - convert a string to lower case
character = lower(string as character)
character = strtolower(string as character)
  • upper() - convert a string to upper case
character = upper(string as character)
character = strtoupper(string as character)
  • proper() - convert a string to lower case with the first character to upper case
character = proper(string as character)
  • ucfirst() - convert a string to lower case with the first character to upper case
character = ucfirst(string as character)

Converting to and from ASCII Values

  • asc() - return the numeric ASCII value of a character.
numeric = asc(string as character)
  • chr() - return the character corresponding to a numeric ASCII value.
character = chr(ASCII as numeric)

Printing Formatted Strings in Recital

Recital provides three functions for the output of strings with 'C' style parameter replacement and formatting.

The printf() function outputs the string to the current output: screen, printer destination, alternate file, etc..

printf(format as character, args [,...])

The sprintf() function returns the formatted string as a character value.

character=sprintf(format as character, args [,...])

The fprintf() function writes the formatted string to an open file using a file pointer reference.

fprintf(file pointer as numeric, format as character, args [,...])

Recital printf Formatting Specifiers

Formatting option Description
%s Convert to character string (similar to using etos() or tostring()
%d For date parameters
%f For floating point numeric parameters
%y For currency parameters
%t For datetime parameters
%T For datetime parameters; character day is also displayed
%l For logical parameters: True, False
%L For logical parameters: Yes, No


Formatting sequences can also contain the following options. These are specified in order, between the '%' and the data type letter.


Formatting option Description
- Left-justify
n Left pad with spaces to width n
n.p Left pad with spaces to width n and include the decimal point and p decimal places (%f only)

Example code printf()

// When %s is specified, the corresponding argument is converted to 
// character format (similar to specifying etos()).
// Widths correspond to the default values, e.g. numerics are 10
printf('It is %s, %s to be more precise\n',year(date()),datetime())
printf('The value of pi is %s\n',pi())
printf('They cost %s per %s\n',$99,100)
printf('Logicals can be %s or %s\n',.T.,.F.)
// Formatting sequences can contain a width, which will left pad with spaces 
printf('Right-justify and pad left: %10s this\n','Like')
// Left justify by placing a '-' directly following the '%' character 
printf('Left-justify and pad right: %-10s this\n','Like')
// %d is for numerics
printf('It is %d\n',year(date()))
// %t and %T are for formating datetime data types.
printf('It is %d, %t to be more precise\n',year(date()),datetime())
printf('It is %d, %T to be even more precise\n',year(date()),datetime())
// %f is for floating point numerics
printf('The value of pi is %f\n',pi())
// Decimal places can also be specified for floating point numerics (%f) 
printf('The value of pi to two decimal places is %4.2f\n',pi())
// %y is for formatting currency data types
printf('They cost %y per %d\n',$99,100)
printf('They cost %y per %d\n',$99,1000)
printf('They cost %y per %d\n',$99,10000)
//%l and %L are for formatting logical datatypes.
printf('Logicals can be %l or %l\n',.T.,.F.)
printf('Logicals can also be %L or %L\n',.T.,.F.)

Example code sprintf()

// When %s is specified, the corresponding argument is converted to 
// character format (similar to specifying etos()).
// Widths correspond to the default values, e.g. numerics are 10
cVAR=sprintf('It is %s, %s to be more precise',year(date()),datetime())
echo cVAR
cVAR=sprintf('The value of pi is %s',pi())
echo cVAR
cVAR=sprintf('They cost %s per %s',$99,100)
echo cVAR
cVAR=sprintf('Logicals can be %s or %s',.T.,.F.)
echo cVAR
// Formatting characters can contain a width, which will left pad with spaces 
cVAR=sprintf('Right-justify and pad left: %10s this','Like')
echo cVAR
// Left justify by placing a '-' directly following the '%' character 
cVAR=sprintf('Left-justify and pad right: %-10s this','Like')
echo cVAR
// %d is for numerics
cVAR=sprintf('It is %d',year(date()))
echo cVAR
// %t and %T are for formating datetime data types.
cVAR=sprintf('It is %d, %t to be more precise',year(date()),datetime())
echo cVAR
cVAR=sprintf('It is %d, %T to be even more precise',year(date()),datetime())
echo cVAR
// %f is for floating point numerics
cVAR=sprintf('The value of pi is %f',pi())
echo cVAR
// Decimal places can also be specified for floating point numerics (%f)
cVAR=sprintf('The value of pi to two decimal places is %4.2f',pi())
echo cVAR
// %y is for formatting currency data types
cVAR=sprintf('They cost %y per %d',$99,100)
echo cVAR
cVAR=sprintf('They cost %y per %d',$99,1000)
echo cVAR
cVAR=sprintf('They cost %y per %d',$99,10000)
echo cVAR
//%l and %L are for formatting logical datatypes.
cVAR=sprintf('Logicals can be %l or %l',.T.,.F.)
echo cVAR
cVAR=sprintf('Logicals can also be %L or %L',.T.,.F.)
echo cVAR

Example code fprintf()

fp=fcreate('fprintf.txt')
// When %s is specified, the corresponding argument is converted to 
// character format (similar to specifying etos()).
// Widths correspond to the default values, e.g. numerics are 10
fprintf(fp,'It is %s, %s to be more precise\n',year(date()),datetime())
fprintf(fp,'The value of pi is %s\n',pi())
fprintf(fp,'They cost %s per %s\n',$99,100)
fprintf(fp,'Logicals can be %s or %s\n',.T.,.F.)
// Formatting characters can contain a width, which will left pad with spaces 
fprintf(fp,'Right-justify and pad left: %10s this\n','Like')
// Left justify by placing a '-' directly following the '%' character 
fprintf(fp,'Left-justify and pad right: %-10s this\n','Like')
// %d is for numerics
fprintf(fp,'It is %d\n',year(date()))
// %t and %T are for formating datetime data types.
fprintf(fp,'It is %d, %t to be more precise\n',year(date()),datetime())
fprintf(fp,'It is %d, %T to be even more precise\n',year(date()),datetime())
// %f is for floating point numerics
fprintf(fp,'The value of pi is %f\n',pi())
// Decimal places can also be specified for floating point numerics (%f)
fprintf(fp,'The value of pi to two decimal places is %4.2f\n',pi())
// %y is for formatting currency data types
fprintf(fp,'They cost %y per %d\n',$99,100)
fprintf(fp,'They cost %y per %d\n',$99,1000)
fprintf(fp,'They cost %y per %d\n',$99,10000)
//%l and %L are for formatting logical datatypes.
fprintf(fp,'Logicals can be %l or %l\n',.T.,.F.)
fprintf(fp,'Logicals can also be %L or %L\n',.T.,.F.)
fclose(fp)

Output

It is       2009, 11/11/2009 11:41:51 AM to be more precise
The value of pi is  3.1415926
They cost $99.0000 per        100
Logicals can be True or False
Right-justify and pad left:       Like this
Left-justify and pad right: Like       this
It is 2009
It is 2009, 11/11/2009 11:41:51 AM to be more precise
It is 2009, Wednesday November 11 2009 11:41:51 to be even more precise
The value of pi is 3.141593
The value of pi to two decimal places is 3.14
They cost $99.0000 per 100
They cost $99.0000 per 1000
They cost $99.0000 per 10000
Logicals can be True or False
Logicals can also be Yes or No

Finding the Length of a Recital String

  • strlen() - return the numeric length of a string
numeric = strlen(string as character)

Converting a String Into an Array

  • astore() - fill an array from a string with separate character elements and return the number of elements
numeric = astore(arrayname, string as character, separator as character)
  • explode() - separate a character string into individual elements and return as an array
array = explode(separator as chatacter, string as character)

Converting an Array into a String

  • astring() - return an array as a character string, with the elements separated by a comma or other specified character
character = astring(arrayname [, separator as character])
  • implode() - return an array as a character string, with the elements separated by a specified character
character = implode(separator as character, arrayname)

Reading a File into a String

  • filetostr() - read the contents of a text file into a character string
character = filetostr(filename as character)

Note: please also see Working with Files and File Systems in Recital.

Writing a String to a File

  • strtofile() - write a string to a file, overwriting or optionally appending to any existing contents
numeric = strtofile(string as character, filename as character [, append as logical | options as numeric])

Note: please also see Working with Files and File Systems in Recital.

String Conversion Functions

  • ctod() - perform character to date conversion
date = ctod(string as character)
  • dtoc() - perform date to character conversion
character = dtoc(date as date)
  • dtos() - perform date to string conversion
YYYYMMDD as character = dtos(date as date)
  • etos() - perform expression to string conversion
character = etos(expression as expression [, length as numeric])
  • mtos() - perform memo to string conversion
character = mtos(memo as memo)
  • stod() - perform string to date conversion
date = stod(YYYYMMDD as character)
  • str() - perform numeric to string conversion
character = str(number as numeric [, length as numeric [, decimals as numeric [, base as numeric]]])
  • strzero() - perform numeric to string with leading zeros conversion
character = strzero(number as numeric [, length as numeric [, decimals as numeric]])
  • tostring() - perform expression to string conversion
character = tostring(expression as expression [, length as numeric])
  • val() - perform string to numeric conversion
numeric = val(string as character)

Removing Leading and Trailing Whitespace from a Recital String

  • alltrim() - remove leading and trailing whitespace
character = alltrim(string as character)
  • ltrim() - remove leading whitespace
character = ltrim(string as character)
  • rtrim() - remove trailing whitespace
character = rtrim(string as character)
  • trim() - remove trailing whitespace
character = trim(string as character)

Padding Recital Strings

character = padc(string as character, length as numeric [, padwith as character])
character = padl(string as character, length as numeric [, padwith as character])
character = lpad(string as character, length as numeric [, padwith as character])
character = padr(string as character, length as numeric [, padwith as character])
character = rpad(string as character, length as numeric [, padwith as character])
character = str_pad(string as character, length as numeric [, padwith as character [, padtype as character]])

Comparing Strings in Recital

The difference() function returns an integer signifying the phonetic difference between two character strings. A return value of 4 represents a close match, and 0 is returned when the two character expression have no letters in common.

numeric = difference(string1 as character, string2 as character)

The inlist() function checks whether a character string exists in a specified list of strings.

logical = inlist(searchfor as character, liststring1 as character, liststring2 as character [,...])

The like() function checks whether a character string matches a pattern, which may include wildcard characters.

logical = like(pattern as character, string as character)

The strcmp() and strcasecmp() functions are used to compare two strings, character by character. They differ in that strcmp() does a case-sensitive comparison and strcasecmp() a case-insensitive comparison.

numeric = strcmp(string1 as character, string2 as character)
numeric = strcasecmp(string1 as character, string2 as character)

The strcmp() and strcasecmp() functions return a numeric value as follows:

Return Value Description
-1 string1 is less than string2
0 string1 and string2 are identical
1 string1 is greater than string2

Extracting Substrings from a Recital String

  • left() - extract substring from left
character = left(string as character, length as numeric)
  • right() - extract substring from right
character = right(string as character, length as numeric)
character = strextract(string as character, startdelimiter as character [, enddelimiter as character 
  [, occurrence as numeric [, flags as numeric]]])
character = strstr(string as character, startdelimiter as character)
character = substr(string as character, startposition as numeric [, length as numeric])

Searching for Characters and Substrings in a Recital String

  • at() | atnext() - search for the first or specified occurrence of a character or substring in a character string and return its starting position
numeric = at(searchforthis as character, inthis as character [, occurrence as numeric])
numeric = atnext(searchforthis as character, inthis as character [, occurrence as numeric])
  • rat() - search for the last or specified occurrence of a character or substring in a character string and return its starting position
numeric = rat(searchforthis as character, inthis as character [, occurrence as numeric])
  • strpos() - search for the first occurrence of a character or substring in a character string and return its starting position
numeric = strpos(searchforthis as character, inthis as character [, starthere as numeric])

Searching for and Replacing Characters and Substrings in a Recital String

  • hardcr() - convert soft carriage returns (ASCII 10) to hard carriage returns (ASCII 13) in a character string
character = hardcr(string as character)
  • occurs() - check how many times a character or substring appears in a character string
numeric = occurs(searchforthis as character, inthis as character)
  • str_replace() - search for and replace text within a character string or memo field
character = str_replace(replacethis as character, withthis as character, inthis as character [, replaced as numeric])
  • strtran() | chrtran() - search for and replace text within a character string or memo field
character = strtran(inthis as character, replacethis as character, withthis as character [, starthere as numeric [, repeat as numeric]])
character = chrtran(inthis as character, replacethis as character, withthis as character [, starthere as numeric [, repeat as numeric]])
  • stuff() - replace text within a character string or memo field
character = stuff(inthis as character, starthere as numeric, characters as numeric, withthis as character)

Miscellaneous String Functions

  • isalpha() - checks if the first character of a character string is alphabetic
logical = isalpha(string as character)
  • isdigit() - checks if the first character of a character string is a digit
logical = isdigit(string as character)
  • islower() - checks if the first character of a character string is in lower case
logical = islower(string as character)
  • isupper() - checks if the first character of a character string is in upper case
logical = isupper(string as character)
  • replicate() - returns a string with the specified number of copies of the specified string
character = replicate(string as character, copies as numeric)