C String Methods String Methods Previously we learned

  • Slides: 14
Download presentation
C++ String Methods

C++ String Methods

String Methods • Previously, we learned how to use the find, at, and length

String Methods • Previously, we learned how to use the find, at, and length methods • There are many other String methods that can be used once a string has been created • The list below provides a small subset of existing string methods/operations 1) int find(str, ind) 2) void insert(ind, str) 3) void replace(ind, n, str) 4) string substr(ind, n) 5) + (Concatenation)

String Method Examples 1) int find(str, ind) - find str starting at index ind

String Method Examples 1) int find(str, ind) - find str starting at index ind string my. String = "arbitrary"; cout << my. String. find("r", 2);

String Method Examples 1) int find(str, ind) - find str starting at index ind

String Method Examples 1) int find(str, ind) - find str starting at index ind string my. String = "arbitrary"; cout << my. String. find("r", 2); a r b i t r a r y 0 1 2 3 4 5 6 7 8 // Prints: 5

String Method Examples 2) void insert(ind, str) – Inserts str at index ind string

String Method Examples 2) void insert(ind, str) – Inserts str at index ind string my. String = "arbitrary"; cout << my. String. insert(3, "DOG");

String Method Examples 2) void insert(ind, str) – Inserts str at index ind string

String Method Examples 2) void insert(ind, str) – Inserts str at index ind string my. String = "arbitrary"; cout << my. String. insert(3, "DOG"); // Prints: arb. DOGitrary a r b i t r a r y 0 1 2 3 4 5 6 7 8

String Method Examples 3) void replace(ind, n, str) – removes n characters starting at

String Method Examples 3) void replace(ind, n, str) – removes n characters starting at index ind and replaces with string my. String = "arbitrary"; cout << my. String. replace(3, 1, "y's ");

String Method Examples 3) void replace(ind, n, str) – removes n characters starting at

String Method Examples 3) void replace(ind, n, str) – removes n characters starting at index ind and replaces with string my. String = "arbitrary"; cout << my. String. replace(3, 1, "y's "); Prints: arby’s trary a r b i t r a r y 0 1 2 3 4 5 6 7 8

String Method Examples 4) string substr(ind, n) – Returns a string of n characters

String Method Examples 4) string substr(ind, n) – Returns a string of n characters starting at index ind string my. String = "arbitrary"; cout << my. String. substr(2, 3);

String Method Examples 4) string substr(ind, n) – Returns a string of n characters

String Method Examples 4) string substr(ind, n) – Returns a string of n characters starting at index ind string my. String = "arbitrary"; cout << my. String. substr(2, 3); // Prints: bit a r b i t r a r y 0 1 2 3 4 5 6 7 8

String Method Examples 5) + (Concatenation) – combines two strings and returns a single

String Method Examples 5) + (Concatenation) – combines two strings and returns a single string 1 = "sword"; string 2 = "fish"; string 3 = string 1 + string 2; cout << string 3 << endl;

String Method Examples 5) + (Concatenation) – combines two strings and returns a single

String Method Examples 5) + (Concatenation) – combines two strings and returns a single string 1 = "sword"; string 2 = "fish"; string 3 = string 1 + string 2; cout << string 3 << endl; // Prints: swordfish

Concatenation and Assignment • += can be used add a new string on to

Concatenation and Assignment • += can be used add a new string on to the end of an existing string 1 = "sword"; string 1 += "fish"; cout << string 1 << endl;

Concatenation and Assignment • += can be used add a new string on to

Concatenation and Assignment • += can be used add a new string on to the end of an existing string 1 = "sword"; string 1 += "fish"; cout << string 1 << endl; // Prints: swordfish