获取两个标记C/C++之间的子字符串
Getting a substring between two tags C/C++
你好,我正在C/C++中创建一个排序分析器。这很简单,我只想使用C/C++从标签"(" and ")"
中获取一个字符串。我知道逻辑就像查找第一个标签,并在找到下一个标签之前,每找到一个字符就增加一个数字。但我的逻辑很糟糕,所以如果有人能给我一个功能,它会有所帮助。
编辑:我看到C/C++字符串函数完全不同,所以只有C++可以。
您似乎不确定C和C++中的字符串处理之间的区别。你的描述似乎意味着你想用C风格来做这件事。
void GetTag(const char *str, char *buffer)
{
buffer[0] = ' ';
char *y = &buffer[0];
const char *x = &str[0];
bool copy = false;
while (x != NULL)
{
if (*x == '(')
copy = true;
else if (*x == ')' && copy)
{
*y = ' ';
break;
}
else if (copy)
{
*y = *x;
y++;
}
++x;
}
}
或者,C++的方法是使用std::字符串,这更安全,因为它不会篡改指针,而且可以说更容易阅读和理解。
std::string GetTag(const std::string &str)
{
std::string::size_type start = str.find('(');
if (start != str.npos)
{
std::string::size_type end = str.find(')', start + 1);
if (end != str.npos)
{
++start;
std::string::size_type count = end - start;
return str.substr(start, count);
}
}
return "";
}
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 构造函数和转换运算符之间的重载解析
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数