2.2.3.15.6 string::FindFind
Description
Searches this string for the match of a character.
This overloaded member function searches this string for the match of a substring.
Syntax
int Find( char ch, int nStart = 0 )
int Find( LPCSTR lpcszSub, int nStart = 0, BOOL bCaseSensitive = TRUE )
Parameters
- ch
- [input]A single character to search for.
- nStart
- [input]The index of the character in the string to begin the search with. If nStart equals 0, search starts from the beginning. The character at nStart is excluded from the search if nStart is not equal to 0.
- lpszSub
- [input]A pointer to a string to search for.
- nStart
- [input]The index of the character in the string to begin the search with. If nStart equals 0, search starts from the beginning. The character at nStart is excluded from the search if nStart is not equal to 0.
- bCaseSensitive
- [input]Perform a case-sensitive searching if TRUE
Return
Returns the zero-based index of the first character in this string object that matches the character or the requested substring. It will return -1 if not found.
Examples
EX1
// Find a single character
void string_Find_ex1()
{
//Search character from the beginning, character is case-sensitive
string str("abcdef");
int nRet = str.Find('c');
out_int("", nRet); // 2
nRet= str.Find('C');
out_int("", nRet); // Can not be found, nRet = -1
//Search character from the specified index, character is case-sensitive
nRet = str.Find('c',1); // Search from the second char
out_int("", nRet); // 2
nRet = str.Find('c',4); // Search from the fifth char
out_int("", nRet); // -1
}
EX2
// Find a string
void string_Find_ex2()
{
//Search a string from the beginning, character is case-sensitive
string str("abcdef");
int nRet = str.Find("de");
out_int("", nRet); // 3
nRet= str.Find("De");
out_int("", nRet); // Can not be found, nRet = -1
//Search a string from the specified index, character is case-sensitive
nRet = str.Find("de",1); //search from the second char
out_int("", nRet); // 3
nRet = str.Find("de",4); //search from the fifth char
out_int("", nRet); // -1
//Search a string from the specified index, character is case-insensitive
nRet = str.Find("DE",1,FALSE);
out_int("", nRet); // 3
}
Remark
See Also
string::FindOneOf, string::ReverseFind
Header to Include
origin.h
|