C++ language

adplus-dvertising
Strings in C++
Previous Home Next

In C++, the std::string class is a general explanation for a string of text. This class reduce more problems introduced with C-style strings with putting the vouchment of flashback masterdom on the string class nay than on the programmer. The class gives few typical string operations as similitude, link, search and replace, and a function for obtaining substrings. that can be fabricated by a C style string, and a C style string can also be obtained from it.

#include <string>
using namespace std;

char greeting[5] = {'N', 'I', 'T', 'I','S','H', '\0'};

char greeting[] = "NITISH";
// Example
#include <iostream>
#include <string>
using namespace std;
#include <boost/regex.hpp>
int main ()
{
boost::regex fullname_regex ("[A–Z]+[a–z]*, [A–Z][a–z]*");
string name;
cout << "Enter you full name: " << flush;
getline (cin, name);
if (! regex_match (name, fullname_regex))
{
cout << "Error: name not entered correctly" << endl;
}
return 0;
}
String class:-

Strings are objects that show series of characters.

C++ provides a easy, secure optional to using char string to handle strings. The C++ string class, portion of the std::namespace, order you to manipulate strings securely.

The standard string class give recourse for like objects by an interface same to that a standard character of bytes, but addition characteristics especially designed to operate by strings of one-byte characters. The string class is an recommence of the basic string class template that uses char like its character type, by default char traits and destinator types.C++ strings allow you to outright initialize, entrust, comparison, and reassign by intuitive operators, also printing and reading , like shown in the example below:

#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "Nitish";
string str2 = "Kumar";
string str3;
int len ;
                               // copy str1 into str3
str3 = str1;
cout << "str3 : " << str3 << endl;
                                      // concatenates str1 and str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
                                    // total lenghth of str3 after concatenation
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}

That this class handles bytes freely of the encoding used: If used to handle series of various byte or character length characters all members of this class, like its iterators, will still operate in terms of bytes

Previous Home Next