Replace values inside string

Asked

Viewed 1,207 times

2

How do I replace values within a string?

To string will be something like:

gabriel = 1.73,derp = 1.80,

Take into account that this structure is: nome = altura, nome = altura,.

In case, I want to replace the height of Openel by 1.75, but I don’t know how to deal with Patterns in c++.

I would like to search for something like 'Gabriel = (%d+)' and replace with 'Gabriel = 1.75'.

Name size may be larger or smaller.

Height can have more numbers and other values.

On Lua it would be like this:

local str = "gabriel = 1.73, derp = 1.80,"
local size = string.match(str, 'gabriel = (.-),')
print(str)
str = string.gsub(str, 'gabriel = '..size..',', 'gabriel = 1.75,')
print(str)

Behold functioning on the ideone.

2 answers

3

In this code, there is the possibility for the user to insert the string to be searched for in the default phrase (contents of the Sword string variable) and the user can also insert the height value to be replaced.

Below is the code for this application:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sNum1;
    string sWord = "Este texto contem gabriel = 1.73,derp = 1.80, essa foi uma busca.";
    string nomeProcurado;
    int iPosInicial, iTam;

    cout << "\nTexto Inicial: " << sWord << "\n";

    cout << "\nString a ser procurada: ";
    cin >> nomeProcurado;
    iPosInicial = sWord.find(nomeProcurado);

    cout << "Digite o novo valor: ";
    cin >> sNum1;

    iTam = nomeProcurado.length();

    sWord.replace(iPosInicial+iTam+3,4, sNum1);

    cout << "\nTexto Alterado: " << sWord << "\n";

    return 0;
}

See it working on ideone: http://ideone.com/33t4NA

2


After editing I saw that the problem was different. Lua and C++ have a pattern - with the pardon of unintentional pun - of different regular expression patterns. It is necessary to make a "translation". And obviously the functions are slightly different. It would look like this:

#include <regex>
#include <string>
#include <iostream>
using namespace std;

int main() {
    string texto = "gabriel = 1.73,derp = 1.80,";
    regex padrao("gabriel = (.*?),");
    cout << regex_replace(texto, padrao, "gabriel = 1.75,");
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I do not like to use Regex and would do otherwise, because this pattern is very simple but is there as you wish.


I think it’s this function you want:

#include <iostream>
using namespace std;

bool replace(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = str.find(from);
    if(start_pos == std::string::npos)
        return false;
    str.replace(start_pos, from.length(), to);
    return true;
}

int main() {
    string texto = "gabriel = 1.73,derp = 1.80,";
    replace(texto, "1.73", "1.75");
    cout << texto;
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • The value may be different from 1.73, so I want to know how to use Patterns (I think that’s it), in c++.

  • So put on Lua a code that does what you want, what you put on it doesn’t do.

  • Okay, now you’re right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.