2

The problem is I don't know the length of the input string. My function can only replace if the input string is "yyyy". I think of the solution is that first, we will try to convert the input string back to "yyyy" and using my function to complete the work.

Here's my function:

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());
    }
}

My main function

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

My expected output should be :

%Y%d
8
  • 1
    Does this answer your question? Replace part of a string with another string Commented Nov 4, 2019 at 5:10
  • I don't think so, because my string has only 1 type of character but does not know its exact length. Commented Nov 4, 2019 at 6:41
  • The answers in that thread are handling variable sizes as well. Maybe, there's more to your use-case. You need to update your question with a complete working example and the inputs and outputs for your string replacement scenarios. Commented Nov 4, 2019 at 6:50
  • Sorry for my bad, I have just edited the post. I hope you can help me out. Commented Nov 4, 2019 at 7:00
  • Check this live run: ideone.com/ngZ8Jo. Isn't this correct? The two groups of toSearch string have been replaced with the replaceStr. Commented Nov 4, 2019 at 7:10

1 Answer 1

5

Use regular expressions.

Example:

#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;
}

Output:

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

If you want to make it even better you can use:

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

That will match any mix of lowercase and upper case for any amount of 'Y's . Example output with that Regex:

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

This is just a simple example of what you can do with regex, I'd recommend to look at the topic on itself, there is plenty of info her in SO and other sites.

Extra: If you want to match before replacing to alternate the replacing you can do something like:

 // 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");
        }
    }
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, your answer works perfectly. I have another question, what about except yy because when it comes to yy I will replace it with %y not %Y. Thanks in advance
Then you would have to do a test to see if your input string matches to yy first. You can do that with regex_match()
I will add another snippet to the answer. It didn't work here.
There is a problem, my input string should be like this yyaa so I guess that your yy_re can't do the job. I think just a little fix with the regex. I mean exactly two y character, but the input string will contain some more characters not just only char y.
No problem. So I edited the snippet with what I came up with only match two yy use %y and if anything else use %Y. Basically, the easiest way I could come up with is just to check the size of the match and do accordingly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.