⚙️ C++

طرق السلاسل النصية - دليل كامل في C++

Here is the translated HTML article in English.

What is it and why? (a real-life analogy)

Imagine you work in a library. You have a book — this is a string of text. But the book can be in different states: new, old, with notes, in different languages. You can do different things with it: rewrite it by hand, find a specific word in it, replace one word with another, convert to uppercase (capital letters), cut it into parts and glue it back together.

In programming, a string is a sequence of characters. It could be a username, a password, an email address, a whole sentence, or even an entire book. String methods are built-in tools that allow us to manipulate this text data: search, replace, change case, trim extra spaces, compare, and much more.

Analogy: Imagine a string is a plasticine sausage. String methods are your hands and tools: you can cut off a piece (substring), attach another piece (concatenation), smooth it out (change case), or find where a certain colored speck is located (substring search).

Why is this needed? Imagine you are writing a program for user registration. You need to:

  • Check that the email contains the '@' symbol (search)
  • Remove extra spaces at the beginning and end of the name (trimming)
  • Capitalize the first letter of the name (case change)
  • Replace all dots in a phone number with hyphens (replacement)

Without string methods, you would have to write dozens of lines of code to perform these simple operations. String methods save you time and make the code clear and reliable.

Theory: how it works internally

In C++, there are two main types of strings: std::string (from the <string> library) and old C-strings (character arrays char[]). We will study std::string, as it is a modern, safe, and convenient way to work with text.

Internally, std::string is an object that stores an array of characters (char) and additional information: the length of the string and the capacity (how much memory is allocated). When you call a method, you are addressing this object and asking it to perform an action.

It is important to understand: a string in C++ is a mutable object. This means that when you call a method that modifies the string (e.g., replace or append), the string is changed in place. This differs from some other languages where strings are immutable.

Each character in a string has its own index (position). Indexing starts at 0. For example, in the string "Hello", the character 'H' has index 0, 'e' — 1, 'l' — 2, and so on.

Important: In C++, strings can contain any characters, including spaces, digits, punctuation marks, and Unicode characters (if using std::wstring or std::u32string). But for simplicity, we will work with regular std::string strings, which work well with Latin and Cyrillic characters in UTF-8 encoding.

Syntax in C++: all forms and variants

To use strings, you need to include the library:

#include <iostream>
#include <string>  // Include the library for working with strings

int main() {
    // Creating a string
    std::string name = "Anna";
    std::string greeting("Hello, world!");
    std::string empty;  // Empty string
    
    // Calling a method — via a dot
    std::cout << name.length() << std::endl;  // Output: 4
    std::cout << greeting.size() << std::endl; // Output: 12
    
    return 0;
}

Now let's look at all the main methods of std::string in a reference table:

Method Description Example call Result
length() / size() Returns the length of the string (number of characters) "Hello".length() 5
empty() Checks if the string is empty (returns true/false) "".empty() true
clear() Clears the string (makes it empty) str.clear() string becomes ""
at(index) Returns the character at the index (with bounds checking) "Cat".at(1) 'a'
operator[] Returns the character at the index (without bounds checking) "Cat"[1] 'a'
front() Returns the first character "Cat".front() 'C'
back() Returns the last character "Cat".back() 't'
append(str) Appends a string to the end (analogous to +=) "Hel".append("lo") "Hello"
insert(pos, str) Inserts a string at the specified position "Hello".insert(3, "p") "Helplo"
erase(pos, count) Removes count characters starting from position pos "Hello".erase(2, 2) "Heo"