Two Ways to Search & Remove Characters from a Text String

Last reviewed: April 30, 1996
Article ID: Q120098
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0
  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, 2.5b, 2.6, 2.6a
  • Microsoft FoxPro for MS-DOS, versions 2.5, 2.5a, 2.5b, 2.6, 2.6a
  • Microsoft FoxPro for Macintosh, version 2.5b, 2.5c, 2.6a
  • Microsoft FoxPro for UNIX, version 2.6

SUMMARY

You can delete characters from a text string in a memory variable or database field. You can use the FoxPro SUBSTR(), ATC(), and STRTRAN() functions to search a text string for a particular value and replace it with another.

MORE INFORMATION

Method One: Use SUBSTR() and ATC() Functions

You can use the ATC() and SUBSTR() functions to eliminate unwanted characters from database fields and memory variables.

For example, look at the following code:

   a="123.45"
   y=SUBSTR(a,1,ATC(".",a)-1)+SUBSTR(a,ATC(".",a)+1)
   b="123 45"
   y=SUBSTR(b,1,ATC(" ",b)-1)+SUBSTR(b,ATC(" ",b)+1)

The variables a and b contain the text strings "123.45" and "123 45" respectively. The code removes the period (.) and the blank space from the variables.

The SUBSTR() function parses the variable into separate pieces based on the value returned by the ATC() function. The ATC() function searches for a string in a variable or field and returns the numeric position of the first character in the string if it is found. If the ATC() function can't locate the string, it returns zero (0).

By combining the SUBSTR() and ATC() functions, you can construct complex statements to alter data in numerous ways.

Method Two: Use STRTRAN() Function

You can use the STRTRAN() function to search and replace characters within a text string using a single command. The following example code shows you how to use STRTRAN() to remove an unwanted period (.) from a text string:

   x="123.4567"
   y=strtran(x,".","")
   ? y  && The variable y now contains "1234567" as a text string.

By default, STRTRAN() looks for all occurrences of the second parameter listed in the function. By adding a numeric value in the third parameter, you can force the function to ignore earlier instances of a character. By adding a fourth numeric parameter, you can control the number of occurrences the function replaces.

The following example code shows you how to remove the second period from a text string while preserving the first occurrence:

   x="123.4567.8910"
   y=strtran(x,".","",2)
   ? y

The variable y returns "123.45678910" as a text string.

REFERENCES

Microsoft FoxPro Commands & Functions 1.02 pp. C4-17; C4-191; C4-189 Microsoft FoxPro Commands & Functions 2.00 pp. C3-156; C3-870; C3-866 Microsoft FoxPro Language Reference 2.5x and 2.60 pp. L3-218; L3-1059; L3-1055


Additional reference words: VFoxWin 3.00 FoxUnix FoxMac FoxDos FoxWin 2.50
2.50a 2.50b
2.50c
2.60 2.60a
KBCategory: kbenv kbprg kbcode
KBSubcategory: FxenvMemory


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 30, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.