Below is an example of a Visual FoxPro function that accepts a single string, extracts the first word (i.e., the characters up to—but not including—the first space), and then checks if that first word is entirely in uppercase. The function returns .T. (true) if the word is all uppercase, and .F. (false) otherwise.
* Function to determine if the first word in a string is all uppercase.
FUNCTION IsFirstWordUpper(tcInput)
LOCAL lcFirstWord, lnSpacePos
* Find the position of the first space.
lnSpacePos = AT(" ", tcInput)
* If a space is found, extract the first word; otherwise, use the entire string.
IF lnSpacePos > 0
lcFirstWord = LEFT(tcInput, lnSpacePos - 1)
ELSE
lcFirstWord = tcInput
ENDIF
* Compare the first word with its uppercase version.
* If they are the same, the word is all in uppercase.
IF lcFirstWord = UPPER(lcFirstWord)
RETURN .T.
ELSE
RETURN .F.
ENDIF
ENDFUNC
* Example usage:
LOCAL lcTest1, lcTest2, llResult
lcTest1 = "HELLO world"
lcTest2 = "Hello world"
llResult = IsFirstWordUpper(lcTest1)
? "Test1 (should be true):", TRANSFORM(llResult)
llResult = IsFirstWordUpper(lcTest2)
? "Test2 (should be false):", TRANSFORM(llResult)
Explanation
-
Extracting the First Word:
- The function uses the
AT() function to locate the first space in tcInput.
- If a space is found (
lnSpacePos > 0), it extracts the substring from the beginning of the string up to the character before the space using LEFT().
- If no space is found, it assumes the entire string is the first word.
-
Checking Uppercase:
- The function converts the first word to uppercase using
UPPER(lcFirstWord) and compares it with the original lcFirstWord.
- If they are equal, it means the first word was already entirely in uppercase, and the function returns
.T. (true). Otherwise, it returns .F. (false).
-
Example Usage:
- The code demonstrates calling the function with two test strings.
- The results are printed using the
? command and TRANSFORM() to display the logical values.
This example adheres to Visual FoxPro syntax and demonstrates how to parse a string, perform a case check, and return a boolean value.