String in C++

What is String?

In programming languages, a “string” is a data type used to represent a sequence of character. Strings can contain letters, numbers, symbols, and spaces.

In C:

A string in C is essentially a one-dimensional array of characters, and it is terminated with a null character ('\0') that indicates the end of the string. This is important because C does not have a dedicated string type like std::string in C++, so the '\0' helps determine the length of the string and where it ends.

Characteristics and Operations associated with strings:

  • Data Type: Strings are a data type that stores a collection of characters. Each character typically represents a single unit of text.
  • Declaration and Initialization: Strings can be declared and initialized  using various syntax.
  • Immutable or Mutable: Strings can be either immutable or mutable, depending on the programming language. In an immutable string, once created, the content cannot be changed. In a mutable string, the content can be modified.

Operations on Strings:

  • Concatenation: Combining two strings to create a new one.
  • Substring: Extracting a portion of a string.
  • Length/Size: Determining the number of characters in a string.
  • Search and Replace: Finding and replacing specific substrings withing a string.
  • Comparison: Checking if two strings are equal or determining their relative order.
  • Conversion: Converting between string and other data types (e.g., converting a number to a string).

Different Types of String in C++

1️⃣ C-Style Strings (Character Arrays)

C++ inherits the concept of C-Style strings, represented.as arrays of characters terminated by a null character ('\0').

These strings are managed manually and need functions like strcpy, strcat, strlen, etc., from the <string.h> library.

Example:

char str[] = "Hello";
char str2[6] = {'H', 'e', 'l', 'l', 'o', '\0'};  // Manually adding the null character

2️⃣ std::string: (C++)

  • C++ provides the std::string class in the <string> header, which simplifies string handling by automatically managing memory and offering built-in functions.
  • std::string supports concatenation, comparison, finding substrings, etc., without needing manual memory management.

Example:

#include <string>
std::string str = "Hello";
str += " World";  // Concatenation

Some useful methods include:

  • length() or size() (to get string length)
  • substr() (to get a substring)
  • find() (to find a substring)
  • append() (to append a string)

3️⃣ std::wstring: (Wide String C++)

A std::wstring is a wide-character string that uses wchar_t to store characters. It is typically used for handling Unicode characters or strings that require more than 1 byte per character.

Example:

#include <string>
std::wstring wstr = L"Hello";  // Wide string literal
  • Wide strings are useful for localization or working with non-ASCII character sets.

4️⃣ std::u16string and std::u32string: (C++)

These represent strings of UTF-16 and UTF-32 encoding, respectively, for handling Unicode characters with different byte lengths.

Example:

#include <string>
std::u16string u16str = u"Hello";  // UTF-16 encoded string
std::u32string u32str = U"Hello";  // UTF-32 encoded string

Raw string literals: preserving the essence

C++11 introduced raw string literals, allowing developers to define strings without escape characters. This is particularly useful for preserving the formatting of text, such as newline characters.