Manipulação de String utilizando a STL
Autor(es): Rene Pegoraro, Pedro Henrique Paiola, Wilson M Yonezawa
Informações obtidas dos sites: - https://web.eecs.umich.edu/~sugih/courses/eecs281/string.html Desenvolvido por: Dr. Mark J. Sebern e - http://www.cplusplus.com/reference/string/string/
#include <string>
using namespace std;
string str1;
string str2 = str1;
string str3 = str1 + str2;
string str4(str2); // alternativa
string str4 = "Hello there";
string str5("Goodbye");
string str6 = 'A'; // incorreto
string str7('A'); // incorreto
string str7(10,'A'); // preenchida
// com 10 caracteres 'A'.
string str8 = "ABCDEFGHIJKL";
string str9(str8,2,5); // inicia str9
// com "CDEFG
Comprimento
size_type length() const;
size_type size() const;
string str = "Hello";
string::size_type len; // int também funciona
len = str.length(); // len == 5
len = str.size(); // len == 5
const char* c_str() const;
string str_cpp=“abcd”;
char str_c[20];
...
strcpy(str_c, str_cpp.c_str());
determinada
string& insert(size_type pos, const string& str);
string str11 = "abcdefghi";
string str12 = "0123";
str11.insert (3, str12); // "abc0123defghi"
string& erase(size_type pos, size_type n);
string str13 = "abcdefghi";
str12.erase(5, 3); // "abcdei"
string& replace(size_type pos, size_type n, const string& str);
string str14 = "abcdefghi";
string str15 = "XYZ";
str14.replace(4, 2, str15); // "abcdXYZghi"
size_type find(const string& str, size_type pos);
– Retorna a última ocorrência
size_type rfind(const string& str, size_type pos);
string str16 = "abcdefghi";
string str17 = "def";
string::size_type pos = str16.find(str17, 0); // retorna 3
size_t find_first_of(const string& str, size_t pos = 0);
size_t find_last_of(const string& str, size_t pos = npos);
senão, retorna o valor string::npos.
std::string str("Um texto vai aqui.");
std::size_t found = str.find_first_of("aeiou"); // retorna 4
posição pos
string substr(size_type pos, size_type n);
string str18 = "abcdefghi"
string str19 = str18.substr(6, 2); // "gh"
string string_one = "Hello";
string string_two;
string_two = string_one;
string string_three;
string_three = "Goodbye";
string string_four;
char ch = 'A';
string_four = ch;
string_four = 'Z';
string str1 = "Hello ";
string str2 = "there";
string str3 = str1 + str2; // "Hello there"
string str1 = "Hello ";
string str4 = str1 + "there";
string str5 = "The End";
string str6 = str5 + '!';
string str1 = "Hello ";
string str2 = "there";
str1 += str2; // "Hello there"
string str1 = "Hello ";
str1 += "there"; // "Hello there"
string str5 = "The End";
str5 += '!'; // "The End!"
string str1 = “Joao”;
string str2 = “Maria”;
if (st1 > str2) {
. . .
string str1 = “Joao”;
if (str1 != “Pedro”) {
. . .
string str10 = "abcdefghi";
char ch = str10[3]; // 'd'
str10[5] = 'X'; // "abcdeXghi"
string str1 = "Hello there";
cout << str1 << endl;
string str1;
cin >> str1;