Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created November 26, 2012 13:24
Show Gist options
  • Save juanfal/4148177 to your computer and use it in GitHub Desktop.
Save juanfal/4148177 to your computer and use it in GitHub Desktop.
Intro to strings… tests
// stringtest.cpp
// juanfc 2009-02-25
// http://www.cplusplus.com/reference/string/string/
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string s = "Juan"; // crea e inicializa
string s2(s), s3=s2; // otras formas de inicializar
cout << s << endl; // a cout
// sumar es concatenar
cout << s + " Falgueras" << endl;
s += " (algo)";
cout << "s: \"" << s << '\"' << endl; // a ver
cout << " \"012345678901234567890\"" << endl; // a ver
// métodos útiles:
cout << "s.length(): " << s.length() << endl;
// ver una letra
cout << "En 6: " << s.at(6) << endl; // at comprueba
cout << "En 6: " << s[6] << endl; // [] no
// métodos potentes
cout << "la 'n' encontrada en pos: " << s.find("n", 2) << endl;
cout << "s.append(\" añadido\"): " << s.append(" añadido") << endl;
cout << "s.insert(10, \" y más\"): " << s.insert(10, " y más") << endl;
cout << "s.replace(10, 6, \"------\"): " << s.replace(10, 6, "------") << endl; // pos, ancho
cout << "s.erase(10, 4): " << s.erase(10, 4) << endl;
cout << "s.substr(6, 7): " << s.substr(6, 7) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment