
This section provides the definitions for string handling functions.
BOOL ChrCmpI(
TCHAR c1,
TCHAR c2
);
Performs a comparison between two characters. The comparison is not case sensitive.
LPTSTR StrChr(
LPCTSTR lpStart,
WORD wMatch
);
Searches a string for the first occurrence of a character that matches the specified character. The comparison is case sensitive.
The comparison assumes lpStart points to the start of a null-terminated string.
LPTSTR StrChrI(
LPCTSTR lpStart,
WORD wMatch
);
Searches a string for the first occurrence of a character that matches the specified character. The comparison is not case sensitive.
The comparison assumes lpStart points to the start of a null-terminated string.
int StrCmpN(
LPCTSTR lpStr1,
LPCTSTR lpStr2,
int nChar
);
#define StrNCmp StrCmpN
Compares a specified number of characters of two strings to determine if they are the same. The comparison is case sensitive. The StrNCmp macro differs from this function in name only.
int StrCmpNI(
LPCTSTR lpStr1,
LPCTSTR lpStr2,
int nChar
);
#define StrNCmpI StrCmpNI
Compares a specified number of characters of two strings to determine if they are the same. The comparison is not case sensitive. The StrNCmpI macro differs from this function in name only.
LPTSTR StrCpyN(
LPCTSTR psz1,
LPCTSTR psz2,
int cchMax
);
#define StrNCpy StrCpyN
Copies a specified number of characters from one string to another. The StrNCpy macro differs from this function in name only.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
//String one
char buffer_1[] = "ABCDEFG";
char *lpStr1;
lpStr1 = buffer_1;
//String two
char buffer_2[] = "HIJKLMN";
char *lpStr2;
lpStr2 = buffer_2;
cout << "The function StrCpyN will allow you to copy (n) numbers"
<< "\nof characters from the string 2 to string 1."
<< "\nString 1 is : " << lpStr1
<< "\nString 2 is : " << lpStr2
<< "\nCopy (n) from 2 onto 1" << endl;
cout << StrCpyN(lpStr1,lpStr2,8)
}
OUTPUT:
- - - - - -
The function StrCpyN will allow you to copy (n) numbers
of characters from the string 2 to string 1.
String 1 is : ABCDEFG
String 2 is : HIJKLMN
Copy (n) from 2 onto 1
HIJKLMN
int StrCSpn(
LPCTSTR lpStr,
LPCTSTR lpSet
);
Searches for the first occurrence of a substring within a string. The search method is case sensitive, and the NULL terminator is included within the search pattern match.
int StrCSpnI(
LPCTSTR lpStr,
LPCTSTR lpSet
);
Searches for the first occurrence of a substring within a string. The search method is not case sensitive, and the NULL terminator is included within the search pattern match.
LPTSTR StrDup(
LPCTSTR lpsz
);
Duplicates a string.
StrDup will allocate storage the size of the original string. If storage allocation is successful, the original string is copied to the duplicate string.
Example:
#include <windows.h>
#include <string.h>
#include <stdio.h>
void main( void )
{
char buffer[] = "This is the buffer text";
char *newstring;
printf( "Original: %s\n", buffer );
newstring = StrDup( buffer );
printf( "Copy: %s\n", newstring );
free( newstring );
}
OUTPUT:
- - - - - -
Original: This is the buffer text
Copy: This is the buffer text
Note This function uses LocalAlloc to allocate storage space for the copy of the string. The calling application must free this memory by calling the LocalFree function on the pointer returned by the call to StrDup.
LPTSTR StrFormatByteSize(
DWORD dw,
LPSTR pszBuf,
UINT cchBuf
);
Converts a numeric value into a string that represents the number expressed as a size value in bytes, KB, MB, or GB, depending on the size.
Example:
532 -> 532 bytes
1340 -> 1.3KB
23506 -> 23.5KB
2400016 -> 2.4MB
2400000000 -> 2.4GB
int StrFromTimeInterval(
LPSTR pszOut,
UINT cchMax,
DWORD dwTimeMS,
int digits
);
Converts a given time interval, in milliseconds, to a string.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
char TimeString[256];
char *pszOut;
pszOut = TimeString;
cout << "The return value from the call to"
<< "\nthe function StrFromTimeInterval will"
<< "\nreturn the number of elements in the buffer: " << endl;
cout << "\nThe return from StrFromTimeInterval is "
<< StrFromTimeInterval(pszOut,30, 34000,30);
cout << "\nThe contents of the TimeString Buffer " << pszOut << endl;
cout << "The return from StrFromTimeInterval is "
<< StrFromTimeInterval(pszOut,30, 74000,3);
cout << "\nThe contents of the TimeString Buffer " << pszOut << endl;
cout << "The return from StrFromTimeInterval is "
<< StrFromTimeInterval(pszOut,30, 74000,2);
cout << "\nThe contents of the TimeString Buffer " << pszOut << endl;
cout << "The return from StrFromTimeInterval is "
<< StrFromTimeInterval(pszOut,30, 74000,1)
<< "\nThe contents of the TimeString Buffer " << pszOut << endl;
}
OUTPUT:
- - - - -
The return value from the call to
the function StrFromTimeInterval will
return the number of elements in the buffer:
The return from StrFromTimeInterval is 7
The contents of the TimeString Buffer 34 sec
The return from StrFromTimeInterval is 13
The contents of the TimeString Buffer 1 min 14 sec
The return from StrFromTimeInterval is 13
The contents of the TimeString Buffer 1 min 10 sec
The return from StrFromTimeInterval is 6
The contents of the TimeString Buffer 1 min
BOOL StrIsIntlEqual(
BOOL fCaseSens,
LPCTSTR lpString1,
LPCTSTR lpString2,
int nChar
);
#define StrIntlEqN(s1, s2, nChar) StrIsIntlEqual(TRUE, s1, s2, nChar)
#define StrIntlEqNI(s1, s2, nChar) StrIsIntlEqual(FALSE, s1, s2, nChar)
Compares a specified number of characters in two strings to determine if they are equal. This function allows you to set the case sensitivity for the search. The StrIntlEqN and StrIntlEqNI macros can be used to call this function. StrIntlEqN performs the comparison with case sensitivity, and StrIntlEqNI performs the comparison without case sensitivity.
LPTSTR StrNCat(
LPTSTR front,
LPCTSTR back,
int cchMax
);
#define StrCatN StrNCat
Copies and appends a specified number of characters from one string onto the end of another string. The StrCatN macro differs from this function in name only.
LPTSTR StrPBrk(
LPCTSTR psz,
LPCTSTR pszSet
);
Searches a string for the first occurrence of a character contained in a specified buffer. This search does not include the null terminator.
LPTSTR StrRChr(
LPCTSTR lpStart,
LPCTSTR lpEnd,
WORD wMatch
);
Searches a string for the last occurrence of a character that matches the specified character. The comparison is case sensitive.
The comparison assumes that lpEnd points to the end of the string.
LPTSTR StrRStrI(
LPCTSTR lpSource,
LPCTSTR lpLast,
LPCTSTR lpSrch
);
Searches for the last occurrence of a substring within a string. The comparison is not case sensitive.
int StrSpn(
LPCTSTR psz,
LPCTSTR pszSet
);
Obtains the length of a substring within a string that consists entirely of characters contained in a specified buffer.
LPTSTR StrStr(
LPCTSTR lpFirst,
LPCTSTR lpSrch
);
Finds the first occurrence of a substring within a string. The comparison is case sensitive.
LPTSTR StrStrI(
LPCTSTR lpFirst,
LPCTSTR lpSrch
);
Finds the first occurrence of a substring within a string. The comparison is not case sensitive.
int StrToInt(
LPCTSTR lpSrc
);
#define StrToLong StrToInt
Converts a decimal string to an integer. The StrToLong macro differs from this function in name only.
BOOL StrToIntEx(
LPCTSTR pszString,
DWORD dwFlags,
int FAR * piRet
);
Converts a decimal or hexadecimal string to an integer.
| STIF_DEFAULT | pszString contains a decimal value. |
| STIF_SUPPORT_HEX | pszString contains a hexadecimal value. |
BOOL StrTrim(
LPTSTR psz,
LPCTSTR pszTrimChars
);
Removes (trims) any leading and trailing characters from a string.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
//String one
TCHAR buffer[] = TEXT("_!ABCDEFG#");
TCHAR trim[] = TEXT("#A!_\0");
cout << "The string before calling StrTrim: ";
cout << buffer;
cout << "\n";
StrTrim(buffer, trim);
cout << "The string after calling StrTrim: ";
cout << buffer;
cout << "\n";
}
OUTPUT:
- - - - - -
The string before calling StrTrim: _!ABCDEFG#
The string after calling StrTrim: BCDEFG
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.