Generalized String Processing

EXTRACT, REPLACE, INSERT, and DELETE are very powerful for referencing and adapting dynamic arrays in mvBASIC. However, they are dependent on the standard delimiters being used within the array. If given a string with different delimiters between its fields, the programmer is forced to use the more generalized string processing functions.

Substring Assignment

A substring is specified by a starting character position and a substring length, separated by commas and enclosed in square brackets. The general syntax for a portion of a string is:

Format

string-expression [n,m]

Parameter(s)

n

Starting column position.

m

Length of the substring.

Description

Using this syntax, substrings can be extracted and replaced like the fields of a dynamic array or the elements of a dimensioned array.

Example

If the string NAME contains "SHAW, GEORGE BERNARD", the variable FIRSTNAME can be assigned "GEORGE" with:

FIRSTNAME = NAME [7,6]

By using column positions, therefore, the EXTRACT function can be simulated for string variables. To simulate the REPLACE, INSERT, and DELETE functions, portions of a string can be assigned values directly. To substitute the string "RALPH" for "GEORGE", for example, the code might read:

NAME[7,6] = “RALPH”

The only thing which is not obvious in manipulating substrings is how to determine the column position and length of the substring. For this purpose, the FIELD, COL1, COL2, LEN, and INDEX functions are crucial to string processing.

The FIELD Function

The FIELD function accepts any character as a delimiter and returns the specified field, thus acting as a generalized EXTRACT. For example, if the string NAME contains "SHAW, GEORGE BERNARD", then the last name "SHAW" can be placed in the variable SURNAME with:

SURNAME = FIELD(NAME , "," , 1)

The COL1, COL2, and LEN Functions

The COL1 and COL2 functions, respectively, return the column positions immediately before and immediately after the last FIELD function. The LEN function returns the number of characters in a string.

For example, if the string NAME contained "SHAW, GEORGE BERNARD", the first name "GEORGE" could be deleted with:

FIRSTNAME = FIELD(NAME, " " , 2)

NAME [COL1( ), COL2( )] = " "

The string NAME now contains "SHAW, BERNARD".

The INDEX Function

The INDEX function returns the column at which a particular substring can be found in a string. This value can be used in a substring assignment statement to replace or delete the substring.

Trimming Spaces

The TRIM, TRIMB, and TRIMF functions each serve to remove all extra spaces from a string. The TRIM function trims all multiple spaces and all spaces at the beginning and at the end of a string. The TRIMB function trims only spaces at the end of a string, and the TRIMF function trims only spaces at the beginning of a string.

Converting Characters

The CONVERT statement can be used to convert every occurrence of a particular character in a string into a different character. The CONVERT statement is particularly useful for converting strings with nonstandard delimiters such as spaces, commas, or colons into dynamic array format. For example, if the string NAME contained "SHAW, GEORGE BERNARD", this name could be incorporated into a file that had the last name and first name separated by attribute marks with:

CONVERT "," TO CHAR(254) IN NAME

See Also

Overview of mvBASIC Statements and Functions

Assignment Statements

Intrinsic Functions

Internal Program Control

External Program Control

Sending Output to the Screen and Printer

Terminal Input

Dynamic Array Processing

Dimensioned Arrays

Reading and Updating File Items

Reading and Writing Tapes or Floppy Disks

Communications

Execution Locks

Compiler Directives

Miscellaneous Statements and Functions

The Error Message Processor