C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C++ programming language » C++ 11

C++ Standard Library the String Class

Learn: In this article we are going to study about the different methods and function of the string class in C++. We study about how to implement string class in out C++ programs.
Submitted by Amit Shukla, on July 27, 2017

String is a predefined data type in C++. This data type is used to store the number of characters. In other words, string is used to store the character array. There is a class in C++ which contains many functions regarding the string. This class is helpful in case when string is very huge, due to various functions it contains we can easily manipulate or we can perform different operations on strings.

We can enable the string class by using header file #include<string.h> in C++. In the latest version of C++ compiler (C++ 14) the header file used to enable string class is #include<string> or #include<cstring>.

Constructors in String class

1. Empty Constructor or default constructor

This constructor is used to assign any variable of data type string. This is used to assign an empty variable of string that’s why it is also known as empty constructor. The following is example of empty constructor.

Example:

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

int main()
{
	string s1;
	return 0;
}

2. Copy constructor

This constructor is used to copy the data from one variable and stores that data into another variable. The following is example of copy constructor.

Example:

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

int main()
{
	string s1="Hello world";
	string s2;
	s2=s1;
	cout<<s2<<endl;
	return 0;
}

Output

Hello world

3. Substring Constructor

This constructor is used to copy the n characters of second string to first string. This constructor is very useful in appending. The syntax of this constructor is:

string s1(s1, int position, int length);

Example:

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

int main()
{
	string s1="Hello world";
	int len=5;
	int pos=6;
	string s2 (s1, pos, len);
	cout<<s2<<endl;
	return 0;
}

Output

world

4. Fill constructor

This constructor is used to fill the any character number of times in a string.

Syntax:

string s1(int size, int character);

here, using this constructor we can fill character in size times in string s1.

Example:

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

int main()
{
	string s2(5,'#');
	cout<<s2<<endl;
	return 0;
}

Output

#####

Functions in String class

There are many functions present in string class. Here we are going to discuss some of important functions.

1. int string.size() and int string.length()

Example:

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

int main()
{
	string s1="Hello world !!!";
	int size=s1.size();
	int length=s1.length();
	cout<<size<<endl;
	cout<<length<<endl;
	return 0;
}

Output

15
15

2. int string.capacity()

This function is used to calculate the allocated space for string in the memory in bytes. String capacity may be or may not be equal to the size or length of string. The return type of this function is also integer.

Example:

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

int main()
{
	string s1="Hello world !!!";
	int capacity= s1.capacity();
	cout<<capacity<<endl;
	return 0;
}

Output

15

3. char string.at (int position)

This function is used to get the character at the specified position in any string. Return type of this function is character.

Example:

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

int main()
{
	string s1="Hello World !!!";
	char c= s1.at(6);
	cout<<c<<endl;
	return 0;
}

Output

W

4. bool string.empty()

This function is used to check whether the string is empty or not. Return type of this function is Boolean. That is if string is empty then the output will be ‘1’ or else the output will be ‘0’.

Example:

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

int main()
{
	string s1="Hello World !!!";
	string s2;
	cout<<"Is string is empty ? "<<s1.empty();
	cout<<endl;
	cout<<"Is string is empty ? "<<s2.empty();
	cout<<endl;
	return 0;
}

Output

Is string is empty ? 0
Is string is empty ? 1

5. stringone.append( stringtwo )

This function is used to append one string into another. The return type of this function is string.

Example:

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

int main()
{
	string s1="Includehelp is ";
	string s2="the best site ";
	string s3= s1.append(s2);
	cout<<s3;
	cout<<endl;
	return 0;
}

Output

Includehelp is the best site

6. string stringone( char *s, int length, int position)

This function is used to copy length character from object *s from given position.

Example:

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

int main ()
{
	char char_arr[20];
	string str ("is best site");
	size_t length = str.copy(char_arr,12,0);
	char_arr[length]='\0';
	cout<< "Include help " <<char_arr<< '\n';
	
	return 0;
}

Output

Include help is best site

7. int string.find( char c)

This function is used to find the specified character from the given string.

Example:

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

int main ()
{
	string s1="Include help ";
	int position;
	position = s1.find('h');
	cout<<"character "<<'h'<<" is found at "<<position;
	cout<<endl;
	return 0;
}

Output

character h is found at 8








COMMENTS