String Functions in C++

There are several string functions present in Standard Template Library in C++ that are used to perform operations on strings. Some of the commonly used string functions are:

1. length() Function

Returns the length of a string.

Example

string str = "w3wiki";
cout << "The length of the string is: " << str.length();

2. substr() Function

It is used to extract a substring from a given string.

Syntax

string substr (size_t pos, size_t len) const;

pos: Position of the first character to be copied.
len: Length of the sub-string.
size_t: It is an unsigned integral type.

Example

C++




#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str = "w3wiki";
  
    // Extracts a substring starting from
    // index 7 with a length of 5
    string sub = str.substr(1, 5);
  
    cout << "Substring: " << sub << endl;
  
    return 0;
}


Output

Substring: eeksf

3. append() Function

Appends a string at the end of the given string.

Example

C++




#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "Geeksfor";
  
    str.append("Geeks");
  
    cout << "Appended string: " << str << endl;
  
    return 0;
}


Output

Appended string: w3wiki

4. compare() Function

It is used to compare two strings lexicographically.

Example

C++




#include <iostream>
#include <string>
using namespace std;
int main() {
    string str1 = "Geeks";
    string str2 = "for";
    string str3 = "Geeks";
      
    int result1 = str1.compare(str2);
    cout << "Comparison result: " << result1 << endl;
  
    int result2 = str1.compare(str3);
    cout << "Comparison result: " << result2 << endl;
      
    return 0;
}


Output

Comparison result: -31
Comparison result: 0

5. empty() Function

It is used to check if a string is empty.

Example

C++




#include <iostream>
#include <string>
using namespace std;
int main() {
    string str1 = "w3wiki";
    string str2 = "";
  
    if (str1.empty()) {
        cout << "str1 is empty" << endl;
    } else {
        cout << "str1 is not empty" << endl;
    }
  
    if (str2.empty()) {
        cout << "str2 is empty" << endl;
    } else {
        cout << "str2 is not empty" << endl;
    }
  
    return 0;
}


Output

str1 is not empty
str2 is empty

C++ Cheatsheet

This is a C++ programming cheat sheet. It is useful for beginners and intermediates looking to learn or revise the concepts of C++ programming. While learning a new language, it feels annoying to switch pages and find different websites for different concepts that are easily understandable. You can learn C++ concepts very easily using this cheat sheet.

C++ is a high-level programming language. It was developed in 1983 by Bjarne Stroustrup at Bell Labs. It is used for developing various applications.

Similar Reads

Let’s create our first C++ program

C++ #include using namespace std; int main() {   cout << "Hello World!";   return 0; }...

Basic Syntax of a Code in C++

...

Comments in C++

// Header files #include // std namespace contains various standard library components using namespace std; // main() function is the starting point of program execution int main() { // This is the section where we write code statements return 0; }...

Variables in C++

Comments can be used for providing an explanation of the code that makes it easier for others to understand the functionality of the code....

Data Types in C++

A variable is a storage location having a name that can hold a value of a specific data type....

Input and Output in C++

Data types are the type of data that a variable can store in a program....

Conditional Statements in C++

1. Input from user: We can take input from the user using cin from the iostream library....

Loop in C++

Conditional statements allow us to control the flow of the program based on certain conditions. It helps us to run a specific section of code based on a condition....

Arrays in C++

Loops are used to repeatedly execute a block of code multiple times....

Multi-Dimensional Arrays in C++

An array is a data structure that allows us to store a fixed number of elements of the same data type in contiguous memory locations....

Vectors in C++

...

References and Pointers

Multi-dimensional arrays are known as arrays of arrays that store similar types of data in tabular form....

Functions

...

String Functions in C++

Vectors are a dynamic array-like data structure that stores elements of the same data type in a contiguous fashion that can resize itself automatically unlike arrays which mean vectors can grow when an element is inserted or shrink when an element is deleted....

Math Functions in C++

...

Object-Oriented Programming in C++

References...

Class and Objects

Functions are the reusable block of a set of statements that performs a specific task. Functions can be used to organize the logic of the program....

Pillars of OOPS

...

File Handling in C++

There are several string functions present in Standard Template Library in C++ that are used to perform operations on strings. Some of the commonly used string functions are:...

Contact Us