Working With String Data in Recital
From Recital Documentation Wiki
Revision as of 16:18, 11 November 2009 by Yvonnemilne (Talk | contribs)
Contents
- 1 Working With String Data in Recital
- 1.1 Changing the Case of a String
- 1.2 Converting to and from ASCII Values
- 1.3 Printing Formatted Strings in Recital
- 1.4 Recital printf Formatting Specifiers
- 1.5 Finding the Length of a Recital String
- 1.6 Converting a String Into an Array
- 1.7 Converting an Array into a String
- 1.8 String Conversion Functions
- 1.9 Removing Leading and Trailing Whitespace from a Recital String
- 1.10 Comparing Strings in Recital
- 1.11 String Comparison Functions Return Value
- 1.12 Accessing and Modifiying Characters in String
- 1.13 Searching for Characters and Substrings in a Recital String
- 1.14 Extracting and Replacing Substrings in Recital
- 1.15 Replacing All Instances of a Word in a Recital String
- 1.16 Miscellaneous String Functions
- 1.17 Summary
Working With String Data in Recital
Changing the Case of a String
- strtolower() - convert a string to lower case
character = strtolower(string as character)
- strtoupper() - convert a string to upper case
character = strtoupper(string as character)
- ucfirst() - convert a string to lower case with the first character of each word in upper case
character = ucfirst(string as character)
Converting to and from ASCII Values
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
Converting an Array into a String
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)