The use of COBOL cripples the mind; its teaching should therefore be regarded as a criminal offense. E.W. Dijkstra

string

Language C++ | Level Intermediate | Category Standard Template Library (STL) | 2015-11-16 22:16:13

0 Comments


Definition

String represent sequences of characters which can be manipulate string operations easily. The string can be used to represent ASCII or wide characters.

Character

Supports all the operations related with strings.
String class is an instantiation of the basic_string class template.
Wstring supports to store UTF-16 text on Windows and UTF-32 on most Unix-like platforms.
It can be constructed from a C-style string.

source Code

								
#include "stdafx.h"

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

int _tmain(int argc, _TCHAR* argv[])
{

	string string1 = "Hello ";
	string string2 = " World!";
	cout << "String one:" << string1 << endl;
	cout << "String two:" << string2 << endl << endl;
	string addtwostrings = string1 + string2;
	cout << "Add two strings :" << addtwostrings << endl;

	string string3 = "Hello ";
	
	cout << "Compare the string1 and string2" << endl;
	if (string1 == string2)
		cout << "string1 and string2 are equal";
	else
		cout << "string1 and string2 are Not equal";

	cout << endl << endl;

	cout << "Compare the string1 and string3" << endl;
	if (string1 == string3)
		cout << "string1 and string3 are equal" << endl;



	return 0;
}

						

Output

    					 
					String one: Hello
					String two: World!
									
					Add two strings :Hello  World!
					Compare the string1 and string2
					string1 and string2 are Not equal

					Compare the string1 and string3
					string1 and string3 are equal
				    					 
					    

When to use

When you want handle the array of characters frequently, you can use strings. You can always prefer to use strings instead of char*. When the string class doesn’t provide any functionality, you can use char array.

0 Comments



Please login to add comments.