INFO: Transposing First Character w/ Last Character in a Field (114498)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft Visual FoxPro for Windows 5.0
  • Microsoft Visual FoxPro for Windows 6.0
  • Microsoft FoxPro for MS-DOS 2.0
  • Microsoft FoxPro for Macintosh 2.6a

This article was previously published under Q114498

SUMMARY

The code in this article describes how to switch the first character with the last character in a field for all records.

MORE INFORMATION

The following code demonstrates an easy way to do this:
* Replaces the field for each record
 REPLACE <fieldname> WITH RIGHT(TRIM(<fieldname>),1)+; 
     SUBSTR(<fieldname>,2,LEN(TRIM(<fieldname>))-2)+LEFT(<fieldname>,1) ; 
     FOR LEN(TRIM(<fieldname>))>1
				
Before the commands are executed, a sample table may look similar to the following:
   <FIELDNAME>
   -----------
   A-0000-Z
   B-0-Y
   C-000000-X
   D-00000-W
				
After the commands are executed, the table looks as follows:
   <FIELDNAME>
   -----------
   Z-0000-A
   Y-0-B
   X-000000-C
   W-00000-D
				
The REPLACE command concatenates the original rightmost character with the a substring of the original value, which omitts the first and last characters, and appends the original leftmost character to that string.

This command works with variable-length character fields. With a few modifications, this code can be quite flexible and useful for string- manipulation purposes and can be adapted to work with other types of fields.

REFERENCES

"Commands & Functions," version 2.0 (MS-DOS), pages C3-701 to C3-704, and C3-659 to C3-660

"Language Reference," version 2.5 (MS-DOS and Windows), pages L3-862 to L3-866, and L3-816 to L3-818

Modification Type:MinorLast Reviewed:3/3/2005
Keywords:kbinfo KB114498 kbAudDeveloper