使用C ++将一个字符串替换为另一个字符串

阮德谭

问题是我不知道输入字符串的长度。仅当输入字符串为“ yyyy”时,我的函数才能替换。我认为解决方案是,首先,我们将尝试将输入字符串转换回“ yyyy”,并使用我的函数完成工作。

这是我的功能:

void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
{
    // Get the first occurrence
    size_t pos = data.find(toSearch);

    // Repeat till end is reached
    while( pos != std::string::npos)
    {
        // Replace this occurrence of Sub String
        data.replace(pos, toSearch.size(), replaceStr);
        // Get the next occurrence from the current position
        pos = data.find(toSearch, pos + replaceStr.size());
    }
}

我的主要功能

std::string format = "yyyyyyyyyydddd";
findAndReplaceAll(format, "yyyy", "%Y");
findAndReplaceAll(format, "dd", "%d");

我的预期输出应为:

%Y%d
加西亚

使用正则表达式。

例:

#include <iostream>
#include <string>
#include <regex>
int main(){
    std::string text = "yyyyyy";
    std::string sentence = "This is a yyyyyyyyyyyy.";
    std::cout << "Text: " << text << std::endl;
    std::cout << "Sentence: " << sentence << std::endl;

    // Regex
    std::regex y_re("y+"); // this is the regex that matches y yyy or more yyyy

    // replacing
    std::string r1 = std::regex_replace(text, y_re, "%y"); // using lowercase
    std::string r2 = std::regex_replace(sentence, y_re, "%Y"); // using upercase 

    // showing result
    std::cout << "Text replace: " <<   r1 << std::endl;
    std::cout <<  "Sentence replace: " << r2 << std::endl;
    return 0;
}

输出:

Text: yyyyyy
Sentence: This is a yyyyyyyyyyyy.
Text replace: %y
Sentence replace: This is a %Y.

如果您想做得更好,可以使用:

// Regex
std::regex y_re("[yY]+");

这将匹配任意数量的'Y'的任何大小写混合形式。该正则表达式的示例输出:

Sentence: This is a yYyyyYYYYyyy.
Sentence replace: This is a %Y.

这只是您可以使用regex进行操作的简单示例,我建议您自己看一下这个主题,在SO和其他网站上有很多关于她的信息。

附加:如果要在替换之前进行匹配以进行替换,则可以执行以下操作:

 // Regex
    std::string text = "yyaaaa";
    std::cout << "Text: " << text << std::endl;
    std::regex y_re("y+"); // this is the regex that matches y yyy or more yyyy


    std::string output = "";
    std::smatch ymatches;
    if (std::regex_search(text, ymatches, y_re)) {
        if (ymatches[0].length() == 2 ) {
            output = std::regex_replace(text, y_re, "%y");
        } else {
            output = std::regex_replace(text, y_re, "%Y");
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用动态中的指针将一个字符串复制到另一个字符串

使用 PHP 将一个字符串与另一个字符串匹配

不使用内置函数将一个字符串中的字符替换为另一个字符串

如何仅使用一个Regex将多个字符串替换为另一个字符串,还是必须使用多个Regex和临时字符串?

在Python中使用Regex将一个字符串替换为另一个字符串:错误:re.error:位置0处的逃逸\ w错误

C ++将一个字符串中的一个字符追加到另一个字符串

如何使用shell脚本将变量中的字符串替换为文件中的另一个字符串

如何使用preg_replace将字符串替换为另一个字符串

Java - 如何使用trim() 方法将空格转换为另一个字符串?

将字符串插入C中的另一个字符串

C ++将字符串除以另一个字符串作为整体

C strcpy复制字符串并添加另一个字符

在C#中用另一个字符串分割一个字符串

如何定义一个字符串大于另一个字符串?(C#)(入门)

排序一个字符串数组,使用另一个字符串数组确定顺序

使用bash脚本如何检查一个字符串是否在另一个字符串内

Java使用嵌套循环在另一个字符串中查找一个字符串出现的次数

使用数组和foreach循环从另一个字符串中获取一个字符串

如何使用另一个字符串作为密码来加密/解密一个字符串?

使用re在另一个字符串中找到一个字符串

使用嵌套循环在另一个字符串中出现一个字符串

使用getch后如何在另一个字符串的末尾连接一个字符串

使用 for 循环在另一个字符串中查找一个字符串

给定一个字符串查找出现的位置并替换为另一个字符串而不使用string.replace()

如何在C ++中从输入中查找字符串并将其替换为另一个字符串?

检查句子中是否存在某些字符串,并使用Python 3.6将其替换为另一个字符串

使用 VBA 在 Word 应用程序中将字符串替换为另一个字符串

使用javascript将文本文件中的多个字符串替换为一个字符串

在C中将一个字符串分成一个子字符串,然后将该子字符串分成另一个字符串