cpp_string_assignment

CPP string Assignment

8.2 CPP string Assignment and Concatenation

string assignment and concatenation capabilities.

Click here to view code image

1 // fig08_01.cpp 2 // Demonstrating string assignment and concatenation. 3 #include <fmt/format.h> 4 #include <iostream> 5 #include <string> 6 7 int main() { 8 std::string s1{“cat”}; 9 std::string s2; // initialized to the empty string 10 std::string s3; // initialized to the empty string 11 12 s2 = s1; // assign s1 to s2 13 s3.assign(s1); // assign s1 to s3 14 std::cout « fmt::format(“s1: {}\ns2: {}\ns3: {}\n\n”, s1, s2, s3); 15 16 s2.at(0) = 'r'; // modify s2 17 s3.at(2) = 'r'; // modify s3 18 std::cout « fmt::format(“After changes:\ns2: {}\ns3: {}”, s2, s3); 19 20 std::cout « “\n\nAfter concatenations:\n”; 21 std::string s4{s1 + “apult”}; // concatenation 22 s1.append(“acomb”); // create “catacomb” 23 s3 += “pet”; // create “carpet” with overloaded += 24 std::cout « fmt::format(“s1: {}\ns3: {}\ns4: {}\n”, s1, s3, s4); 25 26 // append locations 4 through end of s1 to 27 // create string “comb” (s5 was initially empty) 28 std::string s5; // initialized to the empty string 29 s5.append(s1, 4, s1.size() - 4); 30 std::cout « fmt::format(“s5: {}”, s5); 31 }

s1: cat s2: cat s3: cat After changes: s2: rat s3: car After concatenations: s1: catacomb s3: carpet s4: catapult s5: comb

Fig. 8.1 Demonstrating string assignment and concatenation.

String Assignment

Lines 8–10 create the strings s1, s2 and s3. Line 12 uses the assignment operator to copy the contents of s1 into s2. Line 13 uses member function assign to copy s1’s contents into s3. This particular version of assign is equivalent to using the = operator, but assign also has many overloads. For details of each, see

Click here to view code image

https://en.cppreference.com/w/cpp/string/basic_string/assign

For example, one overload copies a specified number of characters, as in

Click here to view code image

target.assign(source, start, numberOfChars);

where source is the string to copy, start is the starting index and numberOfChars is the number of characters to copy.

Accessing String Elements By Index

Lines 16–17 use the string member function at to assign 'r' to s2 at index 0 (forming “rat”) and to assign 'r' to s3 at index 2 (forming “car”). You also can use the member function at to get the character at a specific index in a string. As with std::array and std::vector, a std::string’s at member function performs range checking and throws an out_of_range exception if the index is not within the string’s bounds. The string subscript operator, [], does not check whether the index is in bounds. This is consistent with its use with std::array and std::vector. You also can iterate through the characters in a string using range-based for as in

for (char c : s3) { cout « c; }

which ensures that you do not access elements outside the string’s bounds.

Accessing String Elements By Index

Line 21 initializes s4 to the contents of s1, followed by “apult”. For std::string, the + operator denotes string concatenation. Line 22 uses member function append to concatenate s1 and “acomb”. Next, line 23 uses the overloaded addition assignment operator, +=, to concatenate s3 and “pet”. Then line 29 appends the string “comb” to empty string s5. The arguments are the std::string to retrieve characters from (s1), the starting index (4) and the number of characters to append (s1.size() - 4).

(Cpp20Deit 2022)

cpp_string_assignment.txt · Last modified: 2024/04/28 03:45 (external edit)