C plus plus programming
June 3, 2026 2 min read

String implementation with all the methods

HS
HEMANT SINGH
Technical Writer & Educator

Header file

#include<string>

Declare a string variable

string name;

#include<iostream>
using namespace std;
#include<string>
int main(){
	char a;
	string name;
	
	cout<<"Enter any character:"<<endl;
	cin>>a;
	
	cin.ignore();
	
	cout<<"Enter your name:"<<endl;
	getline(cin, name);

	cout<<a<<endl;
	cout<<name<<endl;
}

String methods

  • .length()/.size(): Return the length of a string
  • s.at(index): Return the character from the string at the given index.
  • s.append("text"): Append new text to the selected string
  • s.substr(start_index, len): Return the part of the string from the given start index with a number of characters
  • s.find("text"): Return the first occurrence of the text in the string.
  • s.replace(pos, len, str): Replace the new string from the given position till the given length.
  • s.insert(pos, str): Insert the string at the index.
  • s.erase(pos, len): Erase the string part from the given position to the given length.

In this category