About Strings

The string functions give applications the means to copy, compare, sort, format, and convert character strings as well as the means to determine the character type of each character in a string. All the string functions support the single-byte, double-byte, and Unicode character sets if these character sets are supported by the operating system on which the application is run.

Security Warning: The incorrect use of string functions can cause security problems for your application. Typically this involves a buffer overrun which can allow a denial of service attack against your application or the injection of executable code from an attacker. The Strsafe functions enable the safer handling of strings and are recommended for better security for your application. For more information on these functions, see Using the Strsafe.h Functions.

This section discusses the following topics.

Comparison with C Run-Time String Functions

Many string functions duplicate or enhance familiar string functions from the standard C run-time (CRT) library. Many of the enhancements enable the string functions to work with Unicode or extended character sets. The following table shows the CRT functions, the Windows functions (that support Unicode, unlike the CRT functions), and the StrSafe functions.

CRT String Function Windows String Function StrSafe Function
strcat lstrcat
StringCchCat
StringCchCatEx
StringCbCat
StringCbCatEx
strcmp lstrcmp (no equivalent function)
strcpy lstrcpy
StringCchCopy
StringCchCopyEx
StringCbCopy
StringCbCopyEx
strlen lstrlen
StringCchLength
StringCbLength

 

The strlen function, for example, always returns the number of bytes in a string, but the lstrlen function returns the number of TCHAR values, which refers to bytes for ANSI versions of the function or WCHAR values for Unicode versions.

The following string functions differ from standard C functions such as tolower and toupper in that they operate on any character in a character set. By using the CharLower function, for example, an application can convert an uppercase U with an umlaut (Ü) to lowercase (ü). For more information about character sets, see Single-byte Character Sets.

Function Description
CharLower Converts a character or string to lowercase.
CharLowerBuff Converts a character string to lowercase.
CharNext Moves to the next character in a string.
CharPrev Moves to the preceding character in a string.
CharUpper Converts a character or string to uppercase.
CharUpperBuff Converts a string to uppercase.

 

The following string functions make determinations about a character based on the semantics of the language selected by the user. These functions are Unicode enabled.

Function Description
IsCharAlpha Determines whether a character is alphabetic.
IsCharAlphaNumeric Determines whether a character is alphanumeric.
IsCharLower Determines whether a character is lowercase.
IsCharUpper Determines whether a character is uppercase.

 

The following table shows the Unicode extensions to the standard C run-time (CRT) functions. As mentioned previously, the StrSafe functions enable safer handling of strings and are recommended for better security for your application.

Standard CRT function String Function StrSafe Function
sprintf wsprintf
StringCchPrintf
StringCchPrintfEx
StringCbPrintf
StringCbPrintfEx
vsprintf wvsprintf
StringCchVPrintf
StringCchVPrintfEx
StringCbVPrintf
StringCbVPrintfEx

 

String Resources

An application that maintains character strings in resources can be translated into new languages with minimum effort. Instead of searching for strings in the source modules, you can simply translate the strings in the resource file and relink the application. In addition, using string resources simplifies creation of Unicode and non-Unicode versions of the application from the same source files.

The LoadString function loads a string resource from an application's executable file. The FormatMessage function loads a string resource and interprets formatting options that may be embedded in the string.

Resources in binary form are stored in Unicode format. When loading resources, applications can use the Unicode version of the resource functions (LoadStringW, for example) to obtain resources as Unicode data.

For 16-bit string resources, 255 characters is the maximum length. For 32-bit string resources, 65535 characters is the maximum length.