分析字符串并交换子字符串
Parse string and swap substrings
如何解析用户给定的字符串,并用新字符串替换所有出现的旧子字符串。我有一个函数要处理,但当涉及到字符串时,我真的很不确定。
void spliceSwap( char* inputStr, const char* oldStr, const char* newStr )
最简单的解决方案是在这里使用谷歌(第一个链接)。还要注意,在C++中,我们更喜欢std::string
而不是const char *
。不要编写自己的std::string
,使用内置的。您的代码似乎更像是C而不是C++!
// Zammbi's variable names will help answer your question
// params find and replace cannot be NULL
void FindAndReplace( std::string& source, const char* find, const char* replace )
{
// ASSERT(find != NULL);
// ASSERT(replace != NULL);
size_t findLen = strlen(find);
size_t replaceLen = strlen(replace);
size_t pos = 0;
// search for the next occurrence of find within source
while ((pos = source.find(find, pos)) != std::string::npos)
{
// replace the found string with the replacement
source.replace( pos, findLen, replace );
// the next line keeps you from searching your replace string,
// so your could replace "hello" with "hello world"
// and not have it blow chunks.
pos += replaceLen;
}
}
相关文章:
- 交换两个字符串时访问正确的内存时,我遇到了分段错误
- 使用 DLL 在 c++ 和 C# 之间交换字符串
- 为什么我在字符串交换程序中得到错误的输出
- 为什么我的交换<字符串,字符串>比标准版本慢得多?
- 排列函数和交换字符串值的问题
- 如何使用C++引用交换两个 C 样式字符串
- 正在交换字符串数组中的字符
- C++交换字符串中两个字符的程序
- 交换字符串数组中的字符串
- 递归字符串交换
- 在c++中使用冒泡排序/交换对字符串进行排序
- 如何交换在函数中作为指针传递的 2 个字符串
- C++:反转数组中的字符串.在两个不同的数组之间交换字符串
- 分析字符串并交换子字符串
- c++字符串交换字符位置
- 使用结束符的位置作为交换空间反转以空结尾的字符串
- 字符串::交换可忽略,因为 std::swap(s1, s2) 就足够了
- c++交换字符串
- 我需要将字符串中的两个值交换为一个(c++)
- c++字符串交换.为什么它会起作用?