在 VC++ 中如何查找字符串
刚才没说清楚,"feifei" 这个是 _TCHAR 类型的,都可以吗?... 展开
字符查找函数
//查找字符串中指定字符第一次出现的位置
LPTSTR StrChr( LPCTSTR lpStart,TCHAR wMatch); //区分大小写
char *strchr( const char *string, int c );
wchar_t *wcschr( const wchar_t *string, wchar_t c );
LPTSTR StrChrI( LPCTSTR lpStart,TCHAR wMatch); //不区分大小写
//查找字符串中指定字符最后一次出现的位置
LPTSTR StrRChr( LPCTSTR lpStart,LPCTSTR lpEnd,TCHAR wMatch); //区分大小写
char *strrchr( const char*string, int c );
wchar *wcsrchr( const wchar_t *string, int c );
LPTSTR StrRChrI( LPCTSTR lpStart,LPCTSTR lpEnd,TCHAR wMatch); //不区分大小写
*注 StrRChr()函数可以通过StrChr()函数和while循环来实现补充:
查找字符串:
_tcsstr(........)
字符串转化为double型数字
_tcstod( const char *nptr, char **endptr )
字符串转化为double型整数(只取整数部分,不取小数)Convert strings to a long-integer value.
_tcstoul ( const char *nptr, char **endptr, int base )
_tcstol ( const char *nptr, char **endptr, int base )
取子字符串
Extracts a substring of length nCount characters from this CStringT object, starting at position iFirst (zero-based).
CStringT Mid(
int iFirst,
int nCount
) const;
CStringT Mid(
int iFirst
) const;
example:
//typedef CStringT < TCHAR, StrTraitATL < TCHAR > > CAtlString;
CAtlString s( _T("abcdef") );
_ASSERT( s.Mid( 2, 3 ) == _T("cde") );
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//ANSI版本
int searchi(const char *buffer, const char *keyword)
{
int k_len, b_len, ch_matches, found, i, j;
b_len = strlen(buffer);
k_len = strlen(keyword);
if (!b_len || !k_len)
return -1;
ch_matches = found = 0;
for (i=0; i<b_len-k_len; i++) {
ch_matches = 0;
for (j=0; j<k_len; j++) {
if (tolower(buffer[i+j]) == tolower(keyword[j])) {
ch_matches++;
if (ch_matches == k_len) {
found++;
i += k_len;
}
}
}
}
return found;
}
int search(const char *buffer, const char *keyword)
{
int k_len, b_len, ch_matches, found, i, j;
b_len = strlen(buffer);
k_len = strlen(keyword);
if (!b_len || !k_len)
return -1;
ch_matches = found = 0;
for (i=0; i<b_len-k_len; i++) {
ch_matches = 0;
for (j=0; j<k_len; j++) {
if (buffer[i+j] == keyword[j]) {
ch_matches++;
if (ch_matches == k_len) {
found++;
i += k_len;
}
}
}
}
return found;
}
//UNICODE版本
int searchiw(const char *buffer, const char *keyword)
{
int k_len, b_len, ch_matches, found, i, j;
char *temp_keyword;
b_len = strlen(buffer);
k_len = strlen(keyword);
if (b_len < 2 || k_len < 2) /* Useless, for words only */
return -1;
if (keyword[0] != ' ' && keyword[k_len-1] != ' ') {
temp_keyword = (char *)calloc(k_len+3, sizeof(char));
if (!temp_keyword)
return -1;
temp_keyword[0] = ' ';
strcat(temp_keyword, keyword);
strcat(temp_keyword, " ");
}
else if (keyword[0] == ' ' && keyword[k_len-1] != ' ') {
temp_keyword = (char *)calloc(k_len+2, sizeof(char));
if (!temp_keyword)
return -1;
strcat(temp_keyword, keyword);
strcat(temp_keyword, " ");
}
else if (keyword[0] != ' ' && keyword[k_len-1] == ' ') {
temp_keyword = (char *)calloc(k_len+2, sizeof(char));
if (!temp_keyword)
return -1;
temp_keyword[0] = ' ';
strcat(temp_keyword, keyword);
}
else {
/* If we get to here and no if statement has executed, keyword already has whitespaces
surrounding it */
temp_keyword = (char *)calloc(k_len+1, sizeof(char));
if (!temp_keyword)
return -1;
strcpy(temp_keyword, keyword);
}
ch_matches = found = 0;
k_len = strlen(temp_keyword); /* Calculate new string length */
for (i=0; i<b_len-k_len; i++) {
ch_matches = 0;
for (j=0; j<k_len; j++) {
if (buffer[i+j] == temp_keyword[j]) {
ch_matches++;
if (ch_matches == k_len) {
found++;
i += k_len;
}
}
}
}
if (temp_keyword != NULL)
free(temp_keyword);
return found;
}
int searchw(const char *buffer, const char *keyword)
{
int k_len, b_len, ch_matches, found, i, j;
char *temp_keyword;
b_len = strlen(buffer);
k_len = strlen(keyword);
if (b_len < 2 || k_len < 2) /* Useless, for words only */
return -1;
if (keyword[0] != ' ' && keyword[k_len-1] != ' ') {
temp_keyword = (char *)calloc(k_len+3, sizeof(char));
if (!temp_keyword)
return -1;
temp_keyword[0] = ' ';
strcat(temp_keyword, keyword);
strcat(temp_keyword, " ");
}
else if (keyword[0] == ' ' && keyword[k_len-1] != ' ') {
temp_keyword = (char *)calloc(k_len+2, sizeof(char));
if (!temp_keyword)
return -1;
strcat(temp_keyword, keyword);
strcat(temp_keyword, " ");
}
else if (keyword[0] != ' ' && keyword[k_len-1] == ' ') {
temp_keyword = (char *)calloc(k_len+2, sizeof(char));
if (!temp_keyword)
return -1;
temp_keyword[0] = ' ';
strcat(temp_keyword, keyword);
}
else {
/* If we get to here and no if statement has executed, keyword already has whitespaces
surrounding it */
temp_keyword = (char *)calloc(k_len+1, sizeof(char));
if (!temp_keyword)
return -1;
strcpy(temp_keyword, keyword);
}
ch_matches = found = 0;
k_len = strlen(temp_keyword); /* Calculate new string length */
for (i=0; i<b_len-k_len; i++) {
ch_matches = 0;
for (j=0; j<k_len; j++) {
if (buffer[i+j] == temp_keyword[j]) {
ch_matches++;
if (ch_matches == k_len) {
found++;
i += k_len;
}
}
}
}
if (temp_keyword != NULL)
free(temp_keyword);
return found;
}
if (_tcschr(tcStr, _T('f') != NULL)
/* 找到 'f' */
else
/* 没找到 */
if (str.Find("f")!=-1)
cout<<"Found!"<<endl;
else cout<<"Not Found!"<<endl;