Exemplos de operações com strings no C++

De MediaWiki do Campus São José
Ir para navegação Ir para pesquisar

Iniciando Strings

Comparando Strings

Ver link

 

#include <string>
#include <iostream>
#include <string.h> // para função de string C

using namespace std;

int main() {
    string texto1 = "delta";
    string texto2 = "betaaaa";

    if(texto1>"beta") {
            cout << "delta é maior..." << endl;
    }
}return 0;
}

Concatenando strings

 
#include <string>
#include <iostream>
#include <string.h> // para função de string C

using namespace std;

int main() {
    string texto1 = "Santa";
    string texto2 = "Catarina";
    string texto;

    texto = texto1 + ' ' + texto2;

    cout << texto << endl;
}

Apagando caracteres da string

Ver link

 
#include <string>
#include <iostream>
#include <string.h> // para função de string C

using namespace std;

int main() {
    string texto1 = "Santa";
    string texto2 = "Catarina";
    string texto;

    texto = texto1 + ' ' + texto2;

    texto.erase(2);

    cout << texto << endl;
}
 

#include <string>
#include <iostream>
#include <string.h> // para função de string C

using namespace std;

int main() {
    string texto1 = "Santa";
    string texto2 = "Catarina";
    string texto;

    texto = texto1 + ' ' + texto2;

    texto.erase(2,3);

    cout << texto << endl;
}