正向顺位取词:先在源字符串中从头出发匹配给出的开头字符串(未找到返回0),然后从匹配到的开头开始匹配结尾(未匹配到则返回从开头开始全部)
例:从123abc456ghsacgha中截取"3a"到"gh"的内容:bc456
返回值:截取所得字符串.
参数:
const char* source:源字符串(被截取的源)
const char* head:开头字符串(用以匹配截取 开头 位置 的字符串)
bool include_head:是否包含截取的开头(返回时前面是否含有开头字符串)
const char* end:结尾字符串(用以匹配截取 停止/收尾 位置 的字符串)
bool include_end:是否包含结尾字符串(返回时后面是否含有结尾字符串)
char* quci3(const char* source, const char* head, bool include_head, const char* end, bool include_end)
{
const unsigned int hl = strlen(head);
const char* hp = strstr(source, head);//匹配开头,返回开头所在指针
char result[2048];
memset(result, '\0', 2048);
if (hp != NULL)//检查输入head参数是否为0(是则返回零)
{
if (end == NULL)//检查输入end参数是否为0(是则返回head之后的字符串)
{
if (include_head == true)//包含开头
{
strcpy_s(result, hp);//从头所在位置往后全部复制
}
else//不包含开头
{
strcpy_s(result, hp + hl);//从头的后面往后全部复制
}
char* lastre = (char*)malloc(strlen(result) + 1);
memset(lastre, '\0', strlen(result) + 1);
strcpy_s(lastre, strlen(result) + 1, result);
return lastre;
}
else//当end非空
{
const char* ep = strstr(hp, end);//从开头字符串位置开始正序顺位匹配结尾字符串
if (ep > hp)
{
if (include_head == true)
{
if (include_end == true)
{
strncpy_s(result, hp, ep - hp + strlen(end));
}
else
{
strncpy_s(result, hp, ep - hp);
}
}
else//当开头和结尾是重合的
{
return 0;//返回零
}
char* lastre = (char*)malloc(strlen(result) + 1);
memset(lastre, '\0', strlen(result) + 1);
strcpy_s(lastre, strlen(result) + 1, result);
return lastre;
}
else if (ep == NULL)//匹配不到结尾字符串位置,从头往后全部复制
{
if (include_head == true)
{
strcpy_s(result, hp);
}
else
{
strcpy_s(result, hp + hl);
}
char* lastre = (char*)malloc(strlen(result) + 1);
memset(lastre, '\0', strlen(result) + 1);
strcpy_s(lastre, strlen(result) + 1, result);
return lastre;
}
else
{
return 0;
}
}
}
else
{
return 0;
}
return 0;
}
来自wjh1动态库其中一个函数
完整wjh1库源码链接https://github.com/IsukaChiyogami/www
高二自学路上积累下来的,有帮助的话动动手指打个星