0% found this document useful (0 votes)
4 views

String Manipulation Functions (1)

The document outlines various string manipulation functions in programming, including concatenation, substring extraction, finding substrings, inserting, erasing, comparing, replacing, checking length, checking if empty, and swapping strings. Each function is accompanied by its purpose, syntax, and an example for clarity. This serves as a comprehensive guide for performing common string operations.

Uploaded by

taimurkhan550471
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

String Manipulation Functions (1)

The document outlines various string manipulation functions in programming, including concatenation, substring extraction, finding substrings, inserting, erasing, comparing, replacing, checking length, checking if empty, and swapping strings. Each function is accompanied by its purpose, syntax, and an example for clarity. This serves as a comprehensive guide for performing common string operations.

Uploaded by

taimurkhan550471
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

String Manipulation Functions

1. Concatenation

 Combines two strings into one.


 Syntax:

string result = str1 + str2; // Using '+' operator


str1 += str2; // Append str2 to str1

 Example:

string str1 = "Hello, ";


string str2 = "World!";
string result = str1 + str2; // "Hello, World!"
str1 += "C++"; // "Hello, C++"

2. Substring

 Extracts a portion of a string.


 Syntax:

string substr = str.substr(start, length);

 Example:

string str = "Hello, World!";


string substr = str.substr(7, 5); // "World"

3. Find

 Locates the position of a substring or character.


 Syntax:

size_t pos = str.find("substring");


size_t pos = str.find('c');

 Example:

string str = "Hello, World!";


size_t pos = str.find("World"); // 7
if (pos != string::npos) {
cout << "Found at position " << pos;
}
4. Insert

 Inserts a substring into the string.


 Syntax:

str.insert(position, "substring");

 Example:

string str = "Hello!";


str.insert(5, ", World"); // "Hello, World!"

5. Erase

 Removes part of the string.


 Syntax:

str.erase(start, length);

 Example:

string str = "Hello, World!";


str.erase(5, 7); // "Hello!"

6. Compare

 Compares two strings lexicographically.


 Syntax:

int result = str1.compare(str2);

Returns 0 if str1 == str2.


o
Returns <0 if str1 < str2.
o
Returns >0 if str1 > str2.
o
 Example:

string str1 = "abc";


string str2 = "def";
int result = str1.compare(str2); // -1 (since "abc" < "def")

7. Replace
 Replaces a portion of the string with another substring.
 Syntax:

str.replace(start, length, "new substring");

 Example:

string str = "Hello, World!";


str.replace(7, 5, "C++"); // "Hello, C++!"

8. Length

 Returns the number of characters in the string.


 Syntax: size_t len = str.length();
 Example: string str = "Hello!";

size_t len = str.length(); // 6

9. Empty

 Checks if the string is empty.


 Syntax: bool isEmpty = str.empty();
 Example:

string str = "";


if (str.empty()) {
cout << "String is empty!";
}

10. Swap

 Swaps the contents of two strings.


 Syntax: str1.swap(str2);
 Example:

string str1 = "Hello";


string str2 = "World";
str1.swap(str2); // str1 = "World", str2 = "Hello"
Summary
Syntax
Function Purpose
Example
Concatenation Combine two strings str1 + str2 or str1 += str2
Substring Extract part of a string str.substr(start, length)
Find Locate substring or character str.find("sub")
str.insert(pos,
Insert Add substring at a position "sub")
str.erase(start,
Erase Remove part of a string length)
Compare Compare two strings str1.compare(str2)
str.replace(start,
Replace Replace part of a string len, "new")
Length Get the length of a string str.length()
Empty Check if string is empty str.empty()
Swap Swap two strings str1.swap(str2)

You might also like