In this C++ tutorial, you will learn how to remove the first character from a given string using string.erase() method, with example programs.

Remove first character from string in C++

To remove the first character from given input string in C++, you can use string erase() member function.

1. Remove first character from the string “HelloWorld” using erase() method in C++

In the following program, we take a string in inputString and an integer in n. We have to repeat the string for n number of times.

Steps

  1. Given an input string in str.
  2. Call erase() method on the input string str, and pass the first argument 0, and second argument 1. The first argument specifies the starting position, and the second argument specifies the length of substring, to be removed from the string. erase() method modifies the original string.
  3. You may print the string str to output.

Program

main.cpp

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

int main() {
    string str = "HelloWorld";

    str.erase(0, 1); // Erase the first character (index 0)
    cout << str << endl;
    
    return 0;
}

Output

elloWorld

Conclusion

In this C++ Tutorial, we learned how to remove the first character from given input string using string erase() method, with examples.