A place to discuss VFP's string handling functions
So you think you know string handling in VFP? Try writing a wiki :-). Trivia: Name 62 string handling functions and commands in VFP, not counting those involving date and time conversion.-- Steven Black
Nice list, but you forgot two :-) :
CAST(eExpr AS datatype [(fieldwidth[,precision])]) is very useful for handling strings with binary data, and an easy way to convert to and from string data types.
STRCONV(cExpr,nConversionFlag) is also extremely useful when dealing with all sorts of encoded data, be it binary, unicode, hexencoded, base64. - Werner
String functions are limited in their maximum length. See String Length. -- Christof Wollenhaupt
Question - I need to be able to find the line numbers within a memo field or string where a particular string can be found. Not only the first occurance, but all occurances. Any ideas?
How about using
ALINES() to put the memo into an array of lines, then use traverse each array element, checking for the string with
ATC() if you aren't case-sensitive.-- Steven Black
ADDBS(cpath) - Adds a backslash (if needed) to a path expression.
ALINES(ArrayName, cExpression [, lTrim]) - Copies each line in a character expression to a corresponding row in an array.
ALLTRIM(cExpr) - Removes leading and trailing blanks from the specified character expression
ASC(cExpr) - Returns the ANSI value for the leftmost character in a character expression.
AT(cSearchExpression, cExpressionSearched [, nOccurrence]) - Returns the beginning numeric position of the first occurrence of a character expression within another character expression, counting from the leftmost character.
AT_C(cSearchExpression, cExpressionSearched [, nOccurrence]) - n-Byte compatible
ATC(cSearchExpression, cExpressionSearched [, nOccurrence]) - Returns the beginning numeric position of the first occurrence of a character expression within another character expression, without regard for the case of these two expressions.
ATCC(cSearchExpression, cExpressionSearched [, nOccurrence]) - Returns the beginning numeric position of the first occurrence of a character expression within another character expression, without regard for the case of these two expressions.
ATCLINE(cSearchExpression, cExpressionSearched) - Returns the line number of the first occurrence of a character expression within another character expression, without regard for the case (upper or lower) of the characters in either expression.
ATLINE(cSearchExpression, cExpressionSearched) - Returns the line number of the first occurrence of a character expression within another character expression, counting from the first line.
CHR(nANSIcode) - Returns the character associated with the specified numeric ANSI code.
CHRTRAN((cSearchedExpression, cSearchExpression, cReplacementExpression) - Replaces each character in a character expression that matches a character in a second character expression with the corresponding character in a third character expression.
My favorite use of this command is to remove unwanted characters from a string by using an empty cReplacementExpression. See the Chrtran topic for a caveat on this with large strings.
lcString = "ABCabc2468"
* get rid of all digits
? chrtran( lcString, "1234567890", "" ) && returns ABCabc
* make sure a string only contains digits
? chrtran( lcString, chrtran( lcString, "1234567890", "" ), "" ) && returns 2468
* the inner
chrtran() evaluates to a string that contains all of the non digit characters
- the outer
chrtran() uses that as the mask to remove all of those characters leaving
- only the digits
- Also if you have numeric value in a a character field you can use the following:
By adding the “.” To the list you can retain the decimal point / place in the converted value.
? !empty( chrtran( lcString, chrtran( lcString, "0123456789", "" ), "" ) )
- Here is a way to tell if there are numbers (or any other set) are contained in a string:
- .t. = there are numbers
- .f. = no numbers in the string.
CHRTRANC(cSearched, cSearchFor, cReplacement) Same as
CHRTRAN() but used for strings with double-byte characters.
DEFAULTEXT(cFileName, cDefault) - Returns a file name with a new extension if one doesn't already exist.
EMPTY(eExpr) - Determines whether an expression evaluates to empty.
FILETOSTR(cFileName)BEAUTIFUL function! Returns the contents of a file as a character string.
FONTMETRIC(nAttribute [, cFontName, nFontSize [, cFontStyle]]) Returns font attributes for the current installed operating system fonts. If you omit cFontName, nFontSize, and cFontStyle, FONTMETRIC( ) returns the attribute for the current font in the active output window.
The corresponding attributes are:
1 Character height in pixels
2 Character ascent (units above baseline) in pixels
3 Character descent (units below baseline) in pixels
4 Leading (space between lines) in pixels
5 Extra leading in pixels
6 Average character width in pixels
7 Maximum character width in pixels
8 Font weight.
9 Italic (0 = no, nonzero = yes)
10 Underlined (0 = no, nonzero = yes)
11 Strikeout (0 = no, nonzero = yes)
12 First character defined in font
13 Last character defined in font
14 Default character (substituted for characters not in font)
15 Word-break character
16 Pitch and family
17 Character set
18 Overhang (extra added width)
19 Horizontal aspect for font device
20 Vertical aspect for font device
INLIST(eExpression1, eExpression2 [, eExpression3 ...]) - Determines whether an expression matches another expression in a set of expressions.
ISLOWER(cExpr) - Determines whether the leftmost character of the specified character expression is a lowercase alphabetic character.
Cool tip: to see if all the characters in a string are lowercase:
function IsAllLower( pcString )
return ( lower( pcString ) == pcPstring )
ISUPPER(cExpr) - Determines whether the leftmost character of the specified character expression is an uppercase alphabetic character.
Cool tip: to see if all the characters in a string are uppercase:
function IsAllUpper( pcString )
return ( upper( pcString ) == pcPstring )
JUSTDRIVE(cPath) - Returns the drive letter from a complete path.
JUSTEXT(cPath) - Returns the three-letter extension from a complete path.
Cool tip: this function basically grabs everything in the string past a RAT( '.' ) so it can be used to grab field names
lcField = justext( this.ControlSource ). It can also be used to work with object hierarchies since the components are . separated.
JUSTFNAME(cFileName) - Returns the file name portion of a complete path and file name.
?JUSTFNAME("C:\temp\junk.txt") && junk.txt
JUSTPATH(cFileName) - Returns the path portion of a complete path and file name.
?JUSTPATH("C:\temp\junk.txt") && c:\temp (note, no trailing "\")
JUSTSTEM(cFileName) - Returns the stem name (the file name before the extension) from a complete path and file name.
?JUSTPATH("C:\temp\junk.txt") && junk
Cool tip: this function basically grabs everything in the string before a RAT( '.' ) so it can be used to grab a table name
lcField = juststem( this.ControlSource ) It can also be used to work with object hierarchies since the components are . separated.
LEFT(cExpression, nExpression) Returns a specified number of characters from a character expression, starting with the leftmost character.
LEFTC() Same as
LEFT() but used for strings with double-byte characters.
LEN(cExpression) Returns the number of characters in a character expression.
LENC() Same as
LEN() but used for strings with double-byte characters.
LIKE(cExpression1, cExpression2) Determines if a character expression matches another character expression. This does wildcard matching using * and ?.
LIKEC() Same as
LIKE() but used for strings with double-byte characters.
LOWER(cExpression) Returns a specified character expression in lowercase letters.
LTRIM(cExpression) Returns the specified character expression with leading blanks removed.
MEMLINES() Returns the number of lines in a memo field or character expression. Sensitive to
MLINE(MemoFieldName, nLineNumber [, nNumberOfCharacters]) Returns a specific line from a memo field or a character string.
_MLINE A ststem memory variable that contains the memo field offset for the
MLINE() returns one line of text from a memo field.
MLINE() stores the location of its memo field offset to the
_MLINE system variable. Use
_MLINE as the second numeric argument in the
MLINE() function to drastically increase the performance of
NORMALIZE(cExpression) Converts a character expression, supplied by a user, into a form that can be compared with Visual FoxPro function return values.
OCCURS(cSearchExpression, cExpressionSearched) Returns the number of times a character expression occurs within another character expression.
PADL(eExpression, nResultSize [, cPadCharacter])
PADR(eExpression, nResultSize [, cPadCharacter]),
PADC(eExpression, nResultSize [, cPadCharacter]) - Returns a string from an expression, padded with spaces or characters to a specified length on the left or right sides, or both.
Cool tip: the
PADx() functions automatically convert numerics to characters.
These functions also truncate (always on the right it seems) to the specified length. padr("abcde",3) returns "abc".
PROPER(cExpression) Returns from a character expression a string capitalized as appropriate for proper names.
RAT(cSearchExpression, cExpressionSearched [, nOccurrence]) Returns the numeric position of the last (rightmost) occurrence of a character string within another character string.
RATC() Same as
RAT() but used for strings with double-byte characters.
REPLICATE(cExpression, nTimes) Returns a character string that contains a specified character expression repeated a specified number of times.
RIGHT(cExpression, nCharacters) Returns the specified number of rightmost characters from a character string.
RIGHTC() Same as RIGHT() but used for strings with double-byte characters.
RTRIM(cExpression) Returns a character string that results from removing the trailing blanks from a character expression.
SET MEMOWIDTH - controls the output width of memos in ? and ? commands. Also controls how the
MLINE() functions split up memo fields or strings.
IMHO the ALINES() function is a much better replacement if you are trying to parse CR/LF delimited lines embedded in a memo or string.
SOUNDEX()AFAIC, this is the most useless function in VFP. Interesting though. It creates a 4 digit code from a word (or other expression) that you can match to other 4 digit codes created by the same function to see if VFP thinks the words sound alike.
The intent is to be able to create indexes that seek strings that sound alike but aren't spelled alike. The algorithm (see http://www.firstct.com/fv/soundex.html) was designed to work on American surnames. It is used a lot in genealogical research.
soundex( "black" ) == soundex( "blacke" ) == soundex( "blake" )
SPACE(nSpaces) Returns a character string composed of a specified number of spaces.
SPACE(0) == ""
STR(nExpression [, nLength [, nDecimalPlaces]]) Returns the character equivalent of a specified numeric expression. See also
STREXTRACT(cSearchExpression, cBeginDelim [, cEndDelim [, nOccurrence[, nFlag]]]]) Retrieves a string between two delimiters.
STRTOFILE((cExpression, cFileName [, lAdditive]) Writes the contents of a character string to a file. See also
STRTRAN(cSearched, cSearchFor [, cReplacement] [, nStartOccurrence] [, nNumberOfOccurrences]) Searches a character expression for occurrences of a second character expression, and then replaces each occurrence with a third character expression.
STUFF(cExpression, nStartReplacement, nCharactersReplaced, cReplacement) Returns a character string created by replacing a specified number of characters in a character expression with another character expression.
Cool Tip: The
LEN(cReplacement) does not have to be equal to nCharactersReplaced. Check the examples in the help topic.
STUFFC() Same as STUFF() but used for strings with double-byte characters.
SUBSTR(cExpression, nStartPosition [, nCharactersReturned]) Returns a character string from the given character expression.
SET TALK ON and nStartPosition is greater than the number of characters in cExpression, Visual FoxPro generates an error message. If
SET TALK OFF, the empty string is returned. That's not right for VFP 6/SP3 - Ibrahim Oezyurt
Cool tip: If you are getting the leftmost characters
LEFT() is faster
SUBSTRC() Same as SUBSTR() but used for strings with double-byte characters.
TRANSFORM(eExpression, [cFormatCodes]) converts any type of expression to a string.
Cool tip: the cFormatCodes argument is only needed when you want specific formatting. Check the
TRANSFORM() topic in the help file to see the default conversion formats.
TRIM(cExpression) Returns the specified character expression with all trailing blanks removed. Identical to
TXTWIDTH(cExpression,cFontName,nFontSize,cFontStyle) Returns the length of a character expression with respect to the average character width for a font. Sample? TXTWIDTH("Hello World","Arial",12,"") vs. TXTWIDTH("Hello World","Courier New",8,"B")
See http://doughennig.blogspot.com/2006/04/forget-txtwidth-use-gdipmeasurestring.html for a more accurate way to measure string lengths. -- Doug Hennig
Note that TXTWIDTH() does not work well with multi-line strings. It returns the width of the entire string, as if all the lines were concatenated, and even assigns an arbritrary width to the CR and LF characters within the text. This is in contrast to the TextWidth and TextHeight methods (methods of a form), which correcly interpret CRs and LFs (but with some limitations). For a more detailed discussion of this point, see Find the height and width of a text string in pixels. Mike Lewis
UPPER(cExpression) Returns the specified character expression in uppercase.
VAL(cExpression)Returns a numeric value from a character expression composed of numbers. Scanning stops with the first characters that doesn't "fit". Given all the characters that can be part of a number (e.g. "e" and "x"), it's a good idea to make sure that the number really is a number.
Category Learning VFP Category Code Samples Category VFP Functions
( Topic last updated: 2012.04.22 12:54:26 PM )