All String Functions in C++ with Examples
This article provides a comprehensive overview of all string functions available in C++ along with illustrative examples. We'll explore functions for manipulation, searching, comparison, and more, allowing you to effectively work with strings in your C++ programs.
Introduction
C++ provides a robust set of string functions to handle various operations on strings. These functions are part of the <string>
header file and offer a convenient way to manipulate, compare, and analyze textual data.
Essential String Functions
Let's delve into some of the most commonly used string functions:
1. length()
and size()
These functions return the length of the string (number of characters).
#include
#include
int main() {
std::string str = "Hello, World!";
std::cout << "Length of string: " << str.length() << std::endl;
std::cout << "Size of string: " << str.size() << std::endl;
return 0;
}
Output:
Length of string: 13
Size of string: 13
2. empty()
This function checks if the string is empty (has zero length).
#include
#include
int main() {
std::string str = "Hello";
std::string emptyStr = "";
if (str.empty()) {
std::cout << "str is empty." << std::endl;
} else {
std::cout << "str is not empty." << std::endl;
}
if (emptyStr.empty()) {
std::cout << "emptyStr is empty." << std::endl;
} else {
std::cout << "emptyStr is not empty." << std::endl;
}
return 0;
}
Output:
str is not empty.
emptyStr is empty.
3. append()
and +=
These functions append a string or a character to the end of the current string.
#include
#include
int main() {
std::string str = "Hello";
str.append(", World!"); // using append()
std::cout << str << std::endl;
str += "!"; // using += operator
std::cout << str << std::endl;
return 0;
}
Output:
Hello, World!
Hello, World!!
4. insert()
This function inserts a string or character at a specific position within the current string.
#include
#include
int main() {
std::string str = "Hello, World!";
str.insert(6, ", there"); // insert at position 6
std::cout << str << std::endl;
return 0;
}
Output:
Hello, there, World!
5. erase()
This function removes characters from the string. You can specify the starting and ending positions for the removal.
#include
#include
int main() {
std::string str = "Hello, World!";
str.erase(6, 6); // erase from position 6 to 11
std::cout << str << std::endl;
return 0;
}
Output:
Hello, !
6. replace()
This function replaces a portion of the string with another string.
#include
#include
int main() {
std::string str = "Hello, World!";
str.replace(7, 5, "C++"); // replace from position 7 to 11 with "C++"
std::cout << str << std::endl;
return 0;
}
Output:
Hello, C++!
7. substr()
This function extracts a substring from the current string. You can specify the starting position and the length of the substring.
#include
#include
int main() {
std::string str = "Hello, World!";
std::string subStr = str.substr(7, 5); // extract from position 7 with length 5
std::cout << "Substring: " << subStr << std::endl;
return 0;
}
Output:
Substring: World
8. find()
and rfind()
These functions search for a specific substring within the string. find()
searches from the beginning, while rfind()
searches from the end.
#include
#include
int main() {
std::string str = "Hello, World!";
size_t pos = str.find("World"); // find "World"
if (pos != std::string::npos) {
std::cout << "Found \"World\" at position: " << pos << std::endl;
} else {
std::cout << "Not found." << std::endl;
}
pos = str.rfind("World"); // find "World" from the end
if (pos != std::string::npos) {
std::cout << "Found \"World\" at position (from end): " << pos << std::endl;
} else {
std::cout << "Not found." << std::endl;
}
return 0;
}
Output:
Found "World" at position: 7
Found "World" at position (from end): 7
9. compare()
This function compares two strings lexicographically (alphabetically).
#include
#include
int main() {
std::string str1 = "Hello";
std::string str2 = "World";
int result = str1.compare(str2);
if (result == 0) {
std::cout << "Strings are equal." << std::endl;
} else if (result < 0) {
std::cout << "str1 is less than str2." << std::endl;
} else {
std::cout << "str1 is greater than str2." << std::endl;
}
return 0;
}
Output:
str1 is less than str2.
Conclusion
This article has provided a comprehensive overview of commonly used string functions in C++. By understanding these functions, you can efficiently manipulate, compare, and analyze strings in your C++ programs. Remember to include the <string>
header file to use these functions effectively.