Difference between revisions of "Working With String Data in Recital"

From Recital Documentation Wiki
Jump to: navigation, search
(Accessing and Modifiying Characters in String)
(Accessing and Modifiying Characters in a Recital String)
Line 335: Line 335:
 
|}
 
|}
  
===Accessing and Modifiying Characters in a Recital String===
+
===Accessing and Modifying Characters in a Recital String===
  
 
===Searching for Characters and Substrings in a Recital String===
 
===Searching for Characters and Substrings in a Recital String===

Revision as of 01:06, 20 January 2010

Working With String Data in Recital

Changing the Case of a String

  • lower() - convert a string to lower case
character = lower(string as character)
  • upper() - convert a string to upper case
character = upper(string as character)
  • proper() - convert a string to lower case with the first character to upper case
character = proper(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)

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

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)
  • stod() - perform string to date conversion
date = stod(YYYYMMDD as character)
  • 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)

Comparing Strings in Recital

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)

String Comparison Functions Return Value

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

Accessing and Modifying Characters in a Recital String

Searching for Characters and Substrings in a Recital String

Extracting and Replacing Substrings in Recital

Replacing All Instances of a Word in a Recital String

Miscellaneous String Functions

Summary