Microsoft KB Archive/86632

{|
 * width="100%"|

Finding the Last Character of a String with MID Function

 * }

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:

<> This macro looks at the variable &quot;zipcode&quot;, reads from character &quot;1&quot; (in this case, the first character is &quot;0&quot;), and extracts five digits.

"If zipcode=01234-5678, &quot;Result&quot; is now &quot;01234&quot;." 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:

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

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

To find the start character of &quot;LETTER.DOC&quot;, 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 &quot;stringname&quot; is the string to be operated on, &quot;start&quot; is the character to begin extracting from, and &quot;range&quot; is the number of characters to be extracted.

The syntax of the LEN command is as follows:

LEN(stringname)

where &quot;stringname&quot; 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 should be typed as displayed.

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

Keywords : kbmacro

Issue type :

Technology :