Common String Functions

C++ provides many built-in string functions that make it easy to work with text data. These functions help in finding length, combining strings, searching text, extracting characters, and more.

What are String Functions?

String functions are predefined functions used to perform operations on strings quickly and efficiently.

Why Use String Functions?

String functions are useful because they:

  • Simplify text processing
  • Reduce code complexity
  • Save programming time
  • Improve code readability

String Library

To use string functions, include:

#include <string>

Common String Functions in C++

1. length()

Returns the number of characters in a string.

#include <iostream>
#include <string>
using namespace std;

int main() {

string text = "Programming";

cout << text.length();

return 0;
}

Output

11

2. size()

Works similar to length().

cout << text.size();

3. append()

Adds one string to another.

#include <iostream>
#include <string>
using namespace std;

int main() {

string a = "Hello ";
string b = "World";

a.append(b);

cout << a;

return 0;
}

Output

Hello World

4. + Operator

Joins two strings.

string full = "C++ " + string("Language");

5. substr()

Extracts part of a string.

#include <iostream>
#include <string>
using namespace std;

int main() {

string text = "Programming";

cout << text.substr(0, 6);

return 0;
}

Output

Progra

6. find()

Searches for text inside a string.

#include <iostream>
#include <string>
using namespace std;

int main() {

string text = "Hello World";

cout << text.find("World");

return 0;
}

Output

6

7. at()

Accesses a specific character safely.

string text = "Cplusplus";

cout << text.at(2);

8. empty()

Checks if a string is empty.

string text = "";

cout << text.empty();

9. clear()

Removes all characters from a string.

string text = "Hello";

text.clear();

10. compare()

Compares two strings.

#include <iostream>
#include <string>
using namespace std;

int main() {

string a = "Ali";
string b = "Ali";

if (a.compare(b) == 0) {
cout << "Equal";
}

return 0;
}

Output

Equal

11. erase()

Removes characters from a string.

string text = "Programming";

text.erase(0, 3);

cout << text;

Output

gramming

12. insert()

Inserts text into a string.

string text = "Hello";

text.insert(5, " World");

cout << text;

Output

Hello World

Advantages of String Functions

  • Easy text manipulation
  • Faster development
  • Cleaner code
  • Built-in optimization

Why String Functions are Important

String functions are important because they:

  • Simplify string operations
  • Improve code efficiency
  • Are widely used in applications
  • Help manage text data easily

Real-Life Example

Think of editing text in a document:

  • Add text
  • Remove text
  • Search words
  • Compare sentences

These tasks are performed using string functions.

Conclusion

Common string functions in C++ make string handling simple and efficient. They help programmers perform text operations quickly using built-in tools and are essential for modern programming applications.

Home » Intermediate C++ > Arrays and Strings > Common String Functions