Finding the Last Character of a String with MID Function

ID: Q86632


The information in this article applies to:
  • Microsoft Word for MS-DOS, versions 5.0, 5.5


SUMMARY

In Microsoft Word versions 5.0 and 5.5 for MS-DOS, the MID function provides the ability to extract a range of characters from a string of text or numbers.


MORE INFORMATION

By itself, the MID function can operate only on a variable of fixed length, or on a string where the value desired is a known number of places from the beginning of the string.

To find characters from the end of the string, use MID with the LEN (length) function.

In the following example, the first five digits of a 10-digit zip code are retrieved:


<<SET result=MID(zipcode,1,5)>> 
This macro looks at the variable "zipcode", reads from character "1" (in this case, the first character is "0"), and extracts five digits.
If zipcode=01234-5678, "Result" is now "01234".
To find the last four digits of the zip code, the LEN function must be introduced with a negative number, as in the following example:

<<SET result=MID(zipcode,LEN(zipcode)-3,4)>> 
If zipcode=01234-5678, LEN(zipcode)-3 returns "5678".
To find the last character in a string of any length, use the value "-0" after the LEN function as follows:

<<SET result=MID(zipcode,LEN(zipcode)-0,4)>> 
If zipcode=01234-5678, LEN(zipcode)-0 returns "8". "Result" is now "5678".
The two functions count characters in different ways. MID counts from left to right, where the first character is "1". LEN with a negative number counts from right to left, starting with "-0".

To find the start character of "LETTER.DOC", count as follows:

with the MID function:           1  2  3  4  5  6  7  8  9 10

-or-                            L  E  T  T  E  R  .  D  O  C

with the MID function with LEN: -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 
The syntax of the MID command is as follows:

MID(stringname,start, range)

where "stringname" is the string to be operated on, "start" is the character to begin extracting from, and "range" is the number of characters to be extracted.

The syntax of the LEN command is as follows:

LEN(stringname)

where "stringname" is the string to be operated upon.

Note: The opening chevron (<<) is created by pressing CTRL+[. The closing chevron (>>) is created by pressing CTRL+]. Single angle brackets ( < and > ) should be typed as displayed.

Additional query words: 5.00 5.50 5.00a 5.00b 5.50a

Keywords : kbmacro
Version : MS-DOS:5.0,5.5
Platform : MS-DOS
Issue type :


Last Reviewed: December 20, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.