版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_17017545/article/details/81098390
1.从一串字符串中提取中间一段,并单独拿出来
char *pStart = NULL,*pMid = NULL,*pEnd = NULL,*t = NULL;
pStart= strstr(pStart, "A");
pEnd = strstr(pStart, "B");
tempLen = pEnd - pStart;
t = (char *)malloc(tempLen + 1);//注意留一个空间给结束符
memcpy(t, pStart, tempLen);
t[tempLen] = '\0';
2.比较字符串,不区分大小写
/*****************************************************************************
* FUNCTION
* Tr_wcsncmp
* DESCRIPTION
* 指定长度比较字符串,不区分大小写
* PARAMETERS
* void
* RETURNS
* void
*****************************************************************************/
#define ISBIGAZ(c) (c >= 'A' && c <= 'Z')
#define ISSMALLAZ(c) (c >= 'a' && c <= 'z')
int Tr_wcsncmp(const char* ch1, const char *ch2, int len)
{
int i=0, j=0 ;
for (i=0; i<len; i++)
{
if(0==Tr_wccharcmp(ch1[i],ch2[i]))
j++;
else
break;
}
if (j == len)
return 0;
else
return 1 ;
}
/*****************************************************************************
* FUNCTION
* Tr_wcscmp
* DESCRIPTION
* 比较字符串,不区分大小写
* PARAMETERS
* void
* RETURNS
* void
*****************************************************************************/
int Tr_wcscmp(const char* ch1, const char *ch2)
{
const char *s1 = ch1;
const char *s2 = ch2;
char c1, c2;
do
{
c1 = *s1++;
c2 = *s2++;
if (c1 == '\0')
return c1 - c2;
}
while (0==Tr_wccharcmp(c1,c2));
return c1 - c2;
}
/*****************************************************************************
* FUNCTION
* Tr_wccharcmp
* DESCRIPTION
* 比较字符,不区分大小写
* PARAMETERS
* void
* RETURNS
* void
*****************************************************************************/
tracker_int8 Tr_wccharcmp(const char ch1, const char ch2)
{
tracker_int8 ret = 1;
if (ch1 == ch2 )
{
ret =0;
}
else if( ISBIGAZ(ch1) || ISSMALLAZ(ch2) )
{
if (ch1 + 32 == ch2 )
ret = 0;
}
else if ( ISBIGAZ(ch2) || ISSMALLAZ(ch1) )
{
if (ch2 + 32 == ch1)
ret = 0;
}
return ret;
}
3.字符串ch1中,查找第n个ch2,返回第n个ch2的头地址
/*****************************************************************************
* FUNCTION
* Tr_n_strstr
* DESCRIPTION
* 字符串ch1中,查找第n个ch2,返回第n个ch2的头地址
* PARAMETERS
* void
* RETURNS
* void
*****************************************************************************/
char* Tr_n_strstr(const char* ch1, const char *ch2, int n)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
char *token_pos = NULL;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
token_pos = strstr(ch1,ch2); //strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
n--;
while (token_pos !=NULL && n>0)
{
token_pos = strstr(token_pos+strlen(ch2),ch2);
n--;
}
return token_pos;
}
其他参考链接
C语言字符串操作总结大全(超详细)
C语言字符串处理专题:字符串连接、字符串合并、字符串截取
C语言中函数返回字符串的四种方法