C/C++搜索并截取字符串(在字符串中寻找匹配“开头“和“结尾“并截取中间内容)例:从123abc456ghsacgha中截取“3a“到“gh“的内容:bc456

正向顺位取词:先在源字符串中从头出发匹配给出的开头字符串(未找到返回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
高二自学路上积累下来的,有帮助的话动动手指打个星

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::vector<std::string> 是C++标准库的容器,用于存储一组字符串。你可以使用它来动态地管理字符串的集合,并且可以方便地进行插入、删除和访问操作。 要截取字符串结尾,你可以使用std::string的substr函数来实现。substr函数接受两个参数,第一个参数是起始位置的索引,第二个参数是要截取的子字符串的长度。如果只指定起始位置而不指定长度,则截取从起始位置到字符串结尾的所有字符。 下面是一个示代码,演示了如何将一个字符串截取为多个子字符串,并将它们存储在std::vector<std::string>: ```cpp #include <iostream> #include <vector> #include <string> int main() { std::string str = "Hello, world! This is a test string."; std::vector<std::string> substrings; // 截取字符串结尾 while (!str.empty()) { substrings.push_back(str); str = str.substr(1); // 从索引1开始截取结尾 } // 输出截取后的子字符串 for (const auto& substring : substrings) { std::cout << substring << std::endl; } return 0; } ``` 运行上述代码,你将得到以下输出: ``` Hello, world! This is a test string. ello, world! This is a test string. llo, world! This is a test string. lo, world! This is a test string. o, world! This is a test string. , world! This is a test string. world! This is a test string. world! This is a test string. orld! This is a test string. rld! This is a test string. ld! This is a test string. d! This is a test string. ! This is a test string. This is a test string. This is a test string. his is a test string. is is a test string. s is a test string. is a test string. is a test string. s a test string. a test string. a test string. test string. test string. est string. st string. t string. string. string. tring. ring. ing. ng. g. . ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值