Common String Methods
append.h
1str.append("added part");
main.cpp
12345678#include <iostream> int main() { std::string str = "Code"; str = str + "finity"; // or str += "finity" std::cout << str << std::endl; }
main.cpp
12345678#include <iostream> int main() { std::string str = "finity"; str = "code" + str + ".com"; std::cout << str << std::endl; }
insert.h
1str.insert(pos, "text to add");
replace.h
1str.replace(start, n, "new text");
erase.h
1str.erase(start, n);
main.cpp
123456789101112131415161718192021#include <iostream> int main() { std::string str = "finity"; str.append(".com"); // finity to finity.com std::cout << str << std::endl; str = "in" + str; // finity.com to infinity.com std::cout << str << std::endl; str.insert(2, "de"); // infinity.com to indefinity.com std::cout << str << std::endl; str.replace(0, 2, "co"); // indefinity.com to codefinity.com std::cout << str << std::endl; str.erase(10, 4); // codefinity.com to codefinity std::cout << str << std::endl; }
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 3. Luku 4
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Suggested prompts:
Can you show examples of how each string method works?
What is the difference between .append() and concatenation with +?
How does the .replace() method work with different values of start and n?
Awesome!
Completion rate improved to 4.35
Common String Methods
Pyyhkäise näyttääksesi valikon
append.h
1str.append("added part");
main.cpp
12345678#include <iostream> int main() { std::string str = "Code"; str = str + "finity"; // or str += "finity" std::cout << str << std::endl; }
main.cpp
12345678#include <iostream> int main() { std::string str = "finity"; str = "code" + str + ".com"; std::cout << str << std::endl; }
insert.h
1str.insert(pos, "text to add");
replace.h
1str.replace(start, n, "new text");
erase.h
1str.erase(start, n);
main.cpp
123456789101112131415161718192021#include <iostream> int main() { std::string str = "finity"; str.append(".com"); // finity to finity.com std::cout << str << std::endl; str = "in" + str; // finity.com to infinity.com std::cout << str << std::endl; str.insert(2, "de"); // infinity.com to indefinity.com std::cout << str << std::endl; str.replace(0, 2, "co"); // indefinity.com to codefinity.com std::cout << str << std::endl; str.erase(10, 4); // codefinity.com to codefinity std::cout << str << std::endl; }
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 3. Luku 4