String Built-in
Function
Here is where your presentation begins
append()
•Definition: Adds more characters to the end of the string.
•Syntax:string.append("additional_string");
Eg;
● std::string str = "Hello";
● str.append(" World");
● // Output: "Hello World"
find()
•Definition: Finds the first occurrence of a substring or character
and returns its index.
•Syntax:string.find("substring_to_find");
Eg;
● std::string str = "Hello, World";
● size_t index = str.find("World");
// Output: 7
swap()
• This function is used to swap the values of 2 string
int main()
{
char str1[10] = "geeks";
char str2[10] = "forgeeks";
swap2(str1, str2);
printf("str1 is %s, str2 is %s", str1, str2);
getchar();
return 0;
}
The syntax of swap() is swap(string1, string2);
size()
• size() function is used to return the length of the string in terms
of bytes.
• It defines the actual number of bytes that conform to the
contents of the String object.
• Syntax: str.size()
.
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
std::cout << "The size of the string is: " << str.size() << std::endl;
return 0;
}
erase()
•Definition: Removes a portion of the string starting at a given
position and for a specified length.
•Syntax: string.erase(start_pos, length);
.
● std::string str = "Hello, World!";
● str.erase(5, 7);
// Output: "Hello"
push_back()
● This function is used to push the passed
character at the end of the string
● Syntax: vector_name.push_back(value);
.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
for (int i = 0; i < numbers.size(); ++i) {
std::cout << numbers[i] << " ";
}
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
10 20 30
pop_back()
● This function is used to pop the last
character from the string
● Syntax: vector_name.pop_back();
#include <iostream>
.
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30};
numbers.pop_back();
for (int i = 0; i < numbers.size(); ++i) {
std::cout << numbers[i] << " ";
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
10 20
clear()
● This function is used to remove all the elements of
the string
● Syntax: container_name.clear();
.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30, 40, 50};
numbers.clear();
if (numbers.empty()) {
std::cout << "The vector is empty!" << std::endl;
} else {
for (int i = 0; i < numbers.size(); ++i) {
std::cout << numbers[i] << " ";
}
}
return 0;
}
---------------------------------------------------------------------------------------------------------------------------
The vector is empty!
replace()
● This function is used to replace each element in the
range [first, last) that is equal to old value with new
value.
Syntax: string_name.replace(start_pos, length, new_string);
.
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
str.replace(7, 5, "C++");
std::cout << str << std::endl;
return 0;
}
----------------------------------------------------------------------------------------------------------------------------
● Hello, C++!