String Manipulation
String Manipulation
String objects are a special type of container, specifically designed to operate with sequences of characters. ANSI C++ provides a new class called string. For using string class, we must include <string.h> String class are used to achieve the following:
Creating string objects Reading string objects from keyboard Displaying string objects to the screen Finding a substring from a string Modifying string objects Comparing string objects Adding string objects Accessing characters in a string Obtaining size of string
Functions
append() assign() at() begin()
cout<<s1=<<s1<<\ns2=<<s2; //inserting a string into another cout<<place s2 inside s1; s1.insert(4,s2);\ cout<<modified string <<s1; //s1=1234abcde5 //removing characters in a string cout<<remove 5 characters from s1; s1.erase(4,5); cout<<now s1=<<s1; //s1=12345 //replace characters in a string cout<<replace middle 3 characters in s2 with s1; s2.replace(1,3,s1); cout<<s2=<<s2; // s2=a12345e
result; s1 = "hello "; s2 = "world"; = s1 + s2; result now contains "hello world"
string result; string s1 = "hello "; string s2 = "world"; result = s1 + s2; // result now contains "hello world"