YouTip LogoYouTip

Cpp Libs String

The C++ Standard Library (Standard Template Library, STL) is one of the core components of C++. It provides a wealth of data structures and algorithms. `` is the header file in the C++ standard library used for handling strings. In C++, a string is a sequence of characters. The `` header file provides the `std::string` class, which is a wrapper around C-style strings and offers safer and easier-to-use string manipulation functions. To use the `` library in a C++ program, you first need to include this header file: #include #include ## Basic Syntax The basic syntax of the `std::string` class is as follows: * Declare a string variable: std::string str; * Initialize a string: std::string str = "Hello, World!"; * Use the `+` operator to concatenate strings: std::string str1 = "Hello, "; std::string str2 = "World!"; std::string result = str1 + str2; ## Common Member Functions The `std::string` class provides many member functions for manipulating strings. Here are some commonly used member functions: * `size()`: Returns the length of the string. * `empty()`: Checks whether the string is empty. * `operator[]`: Accesses characters in the string by index. * `substr()`: Gets a substring. * `find()`: Finds the position of a substring within the main string. * `replace()`: Replaces certain characters in the string. ## Example Here's a simple example using the `` library, including the output results: ## Example #include #include int main(){ // Declare and initialize a string std::string greeting ="Hello, World!"; std::cout<<"Greeting: "<< greeting << std::endl; // Use size() to get the length of the string std::cout<<"Length of the greeting: "<< greeting.size()<< std::endl; // Use empty() to check if the string is empty std::cout<<"Is the greeting empty? "<<(greeting.empty()?"Yes":"No")<< std::endl; // Use operator[] to access a character at a specific position std::cout<<"Character at position 7: "<< greeting<< std::endl; // Use substr() to get a substring std::string sub = greeting.substr(7, 5); std::cout<<"Substring from position 7 with length 5: "<< sub << std::endl; // Use find() to find a substring std::cout<<"Position of 'World' in the greeting: "<< greeting.find("World")<< std::endl; // Use replace() to replace part of the string // Replace 'World' with 'C++' std::string modified = greeting; std::string::size_type pos = modified.find("World"); if(pos != std::string::npos){ modified.replace(pos, 5, "C++");// Replace 5 characters starting from position pos with "C++" } std::cout<<"Modified greeting: "<< modified << std::endl; return 0; } Output: Length of the greeting: 13Is the greeting empty? NoCharacter at position 7: W Substring from position 7 with length 5: WorldPosition of 'World' in the greeting: 7Modified greeting: Hello, C++! ## Summary Table of std::string Member Functions Below is a summary of common `std::string` member functions: | Function Name | Description | Example Code | | --- | --- | --- | | `size()` | Returns the length of the string (number of characters). | `std::cout << str.size();` | | `length()` | Same as `size()`, returns the length of the string. | `std::cout << str.length();` | | `empty()` | Checks whether the string is empty. | `std::cout << (str.empty() ? "Yes" : "No");` | | `operator[]` | Accesses the character at a specified position in the string. | `std::cout << str;` | | `at()` | Accesses the character at a specified position in the string (with bounds checking). | `std::cout << str.at(0);` | | `substr()` | Returns a substring starting from a specified position. | `std::string sub = str.substr(0, 5);` | | `find()` | Finds the position of a substring within the string. | `std::cout << str.find("sub") << std::endl;` | | `rfind()` | Finds the position of a substring starting from the end of the string. | `std::cout << str.rfind("sub") << std::endl;` | | `replace()` | Replaces part of the string. | `str.replace(pos, length, "new_substring");` | | `append()` | Appends content to the end of the string. | `str.append(" more");` | | `insert()` | Inserts content at a specified position. | `str.insert(pos, "inserted");` | | `erase()` | Deletes characters or a substring at a specified position. | `str.erase(pos, length);` | | `clear()` | Clears the string. | `str.clear();` | | `c_str()` | Returns a C-style string (null-terminated). | `const char* cstr = str.c_str();` | | `data()` | Returns a pointer to the character data (C++11 and later). | `const char* data = str.data();` | | `compare()` | Compares two strings. | `int result = str.compare("other");` | | `find_first_of()` | Finds the position of the first character that matches any of the given characters. | `size_t pos = str.find_first_of("aeiou");` | | `find_last_of()` | Finds the position of the last character that matches any of the given characters. | `size_t pos = str.find_last_of("aeiou");` | | `find_first_not_of()` | Finds the position of the first character that does not match any of the given characters. | `size_t pos = str.find_first_not_of("aeiou");` | | `find_last_not_of()` | Finds the position of the last character that does not match any of the given characters. | `size_t pos = str.find_last_not_of("aeiou");` | ## Example #include #include int main(){ std::string str ="Hello, World!"; // size() std::cout<<"Length: "<< str.size()<< std::endl; // empty() std::cout<<"Is empty? "<<(str.empty()?"Yes":"No")<< std::endl; // operator[] std::cout<<"First character: "<< str<< std::endl; // at() std::cout<<"Character at position 7: "<< str.at(7)<< std::endl; // substr() std::string sub = str.substr(7, 5); std::cout<<"Substring from position 7 with length 5: "<< sub << std::endl; // find() size_t pos = str.find("World"); std::cout<<"Position of 'World': "<< pos << std::endl; // replace() str.replace(pos, 5, "C++"); std::cout<<"Modified string: "<< str << std::endl; // append() str.append(" How are you?"); std::cout<<"Appended string: "<< str << std::endl; // insert() str.insert(7, " Beautiful"); std::cout<<"String after insert: "<< str << std::endl; // erase() str.erase(7, 10); std::cout<<"String after erase: "<< str << std::endl; // clear() str.clear(); std::cout<<"String after clear: "<<(str.empty()?"Empty":"Not empty")<< std::endl; // c_str() str ="Hello, C++!"; const char* cstr = str.c_str(); std::cout<<"C-style string: "<< cstr << std::endl; // compare() int cmp = str.compare("Hello, C++!"); std::cout<<"Comparison result: "<< cmp << std::endl; // find_first_of() size_t pos_first_vowel = str.find_first_of("aeiou"); std::cout<<"First vowel at position: "<< pos_first_vowel << std::endl; // find_last_of() size_t pos_last_vowel = str.find_last_of("aeiou"); std::cout<<"Last vowel at position: "<< pos_last_vowel << std::endl; // find_first_not_of() size_t pos_first_non_vowel = str.find_first_not_of("aeiou"); std::cout<<"First non-vowel at position: "<< pos_first_non_vowel << std::endl; // find_last_not_of() size_t pos_last_non_vowel = str.find_last_not_of("aeiou"); std::cout<<"Last non-vowel at position: "<< pos_last_non_vowel << std::endl; return 0; } Output: Length: 13Is empty? NoFirst character: H Character at position 7: W Substring from position 7 with length 5: WorldPosition of 'World': 7Modified string: Hello, C++!Appended string: Hello, C++! How are you?String after insert: Hello, BeautifulC++! How are you?String after erase: Hello, C++! How are you?String after clear: Empty C-style string: Hello, C++!Comparison result: 0First vowel at position: 1Last vowel at position: 4First non-vowel at position: 0Last non-vowel at position: 10
← Cpp Libs CtimeCpp Libs Valarray β†’