Strings and String Manipulation in C++

Strings
C++ provides convenient and powerful tools to manipulate strings. The std::string class is a standard representation for a string of text. This class alleviates many of the problems introduced by C-style strings by putting the onus of memory ownership on the string class rather than on the programmer. The class provides some typical string operations like comparison, concatenation, find and replace, and a function for obtaining substrings. It can be constructed from a C-style string, and a C-style string can also be obtained from it.
String class
Strings are objects that represent sequences of characters.
The standard string class provides support for such objects with an interface similar to that of standard containers, but adding features specifically designed to operate with strings of characters.
The string class is an instantiation of the basic_string class template that uses char as the character type, with its default char_traits and allocator types (see basic_string for more info on the template).
Member types
value_type char
traits_type char_traits<char>
allocator_type allocator<char>
reference char&
const_reference const char&
pointer char*
const_pointer const char*
iterator random access iterator to char (convertible to const_iterator)
const_iterator random access iterator to const char
reverse_iterator <iterator> reverse iterator<iterator>
const_reverse_iterator  reverse iterator<const_iterator>
difference_type   ptrdiff_t
size_type   size_t
Member functions
(constructor) Construct string object (public member function )
(destructor) String destructor (public member function )
operator= String assignment (public member function )

Iterators: -
begin Return iterator to beginning (public member function )
end Return iterator to end (public member function )
rbegin Return reverse iterator to reverse beginning (public member function )
rend Return reverse iterator to reverse end (public member function )
cbegin  Return const_iterator to beginning (public member function )
cend  Return const_iterator to end (public member function )
crbegin  Return const_reverse_iterator to reverse beginning (public member function )
crend  Return const_reverse_iterator to reverse end (public member function )
Capacity:: -
size Return length of string (public member function )
length Return length of string (public member function )
max_size Return maximum size of string (public member function )
resize Resize string (public member function )
capacity Return size of allocated storage (public member function )
reserve Request a change in capacity (public member function )
clear Clear string (public member function )
empty Test if string is empty (public member function )
shrink_to_fit  Shrink to fit (public member function )
Element access: -
operator[] Get character of string (public member function )
at Get character in string (public member function )
back  Access last character (public member function )
front Access first character (public member function )

Modifiers: -
operator+= Append to string (public member function )
append Append to string (public member function )
push_back Append character to string (public member function )
assign Assign content to string (public member function )
insert Insert into string (public member function )
erase Erase characters from string (public member function )
replace Replace portion of string (public member function )
swap Swap string values (public member function )
pop_back Delete last character (public member function )
String operations: -
c_str Get C string equivalent (public member function )
data Get string data (public member function )
get_allocator Get allocator (public member function )
copy Copy sequence of characters from string (public member function )
find Find content in string (public member function )
rfind Find last occurrence of content in string (public member function )
find_first_of Find character in string (public member function )
find_last_of Find character in string from the end (public member function )
find_first_not_of Find absence of character in string (public member function )
find_last_not_of Find non-matching character in string from the end (public member function )
substr Generate substring (public member function )
compare Compare strings (public member function )
Member constants
npos : - Maximum value for size_t (public static member constant )
Non-member function overloads
operator+ Concatenate strings (function )
relational operators Relational operators for string (function )
swap Exchanges the values of two strings (function )
operator>> Extract string from stream (function )

Copying: -
memcpy Copy block of memory (function )
memmove Move block of memory (function )
strcpy Copy string (function )
strncpy Copy characters from string (function )

Concatenation: -
strcat Concatenate strings (function )
strncat Append characters from string (function )
Comparison: -
memcmp Compare two blocks of memory (function )
strcmp Compare two strings (function )
strcoll Compare two strings using locale (function )
strncmp Compare characters of two strings (function )
strxfrm Transform string using locale (function )

Searching: -
memchr Locate character in block of memory (function )
strchr Locate first occurrence of character in string (function )
strcspn Get span until character in string (function )
strpbrk Locate characters in string (function )
strrchr Locate last occurrence of character in string (function )
strspn Get span of character set in string (function )
strstr Locate substring (function )
strtok Split string into tokens (function )

Other:-
memset Fill block of memory (function )
strerror Get pointer to error message string (function )
strlen Get string length (function)

Macros
NULL: - Null pointer (macro )
Types
size_t: - Unsigned integral type (type)
More about String Functions
Name Description
isalnum() Checks if the given argument is alphanumeric character.
isalpha() Checks if the given argument is an alphabet.
isdigit() Checks if the given argument is a digit(0-9)
ispunct() Checks if the given argument is a punctuation character
iscntrl() Checks if the given argument is a control character
isspace() Checks if the given argument is a space character
isupper() Checks if the given argument is a uppercase alphabets
isgraph() Checks if the given argument is a graphical character.
islower() Checks if the given argument is a lowercase alphabets
isprint () Checks if the given argument is a printable character
isxdigit() Checks if the given argument is a hexadecimal digit
strcpy() Copies the content of a string to another.
strchr() Returns a pointer value for the first occurrence of character.
strcat() Appends a copy of the source string to the destination string
strcoll()
strncmp() Compares two strings lexicographically until the characters differ or a null terminated string is encountered
strncpy() Copies upto the characters specified in the "count" from string 2 to string 1
strpbrk() Returns a pointer to the first character in string that matches any character another string.
strrchr() Returns the last occurrence of a character in the string.
strstr() Returns a pointer to the first occurence of string2 in string1.
strspn() Returns the initial subtring of string1 which consists only of characters contained in string2.
strtok() Returns a pointer to next token in the string.
strxfrm() Transforms string2, so that it can be used by "strcmp()".
strerror() Returns the value of errnum as a string a string describing the error.
strlen() Returns the length of a string.
strcspn() Returns the index of the first character in the string1 that matches any of the characters in string2.
tolower() Returns the lowercase of the parameter.
toupper() Returns the uppercase of the parameter.
memset() Sets the specified number of bytes of memory pointed by the pointer to the specified value.
memchr() Searches the array for first occurence of a character.
memmove() Copies the specified count of characters from the an array to another.
memcpy() Copies the specified number of characters from the array2 to array1.
memcmp() Compares the specified number of characters from the array1 to array2.

Example: -
#include < iostream >
#include < string>
using namespace std;
int main ()
{
string str1 = "Hello";
string str2 = "World";
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;
}
Output: -
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10


Free Web Hosting