🔷 C#

String-Methoden – Vollständiger Leitfaden in C# (.NET): Eine ausführliche Anleitung

```html

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

Imagine you are writing a letter to a friend. First, you just type the text: "Hello, how are you?". But then you want to improve it: capitalize the first letter, replace the word "Hello" with "Greetings", check for extra spaces, or find out how many characters are in the letter. All of these are string operations.

A string in programming is simply a sequence of characters: letters, numbers, punctuation marks, and spaces. In C#, strings are not just a set of characters, but a special data type string (lowercase) or its full name System.String.

String methods are built-in tools that allow you to manipulate text. Instead of writing complex algorithms to find a word or replace letters, you simply call a ready-made method. It's like having a Swiss Army knife for working with text: there are scissors (to trim), a magnifying glass (to find), glue (to join), and a ruler (to measure).

Important: Strings in C# are immutable. This means that when you call a method that "changes" a string, a new string is actually created, while the old one remains unchanged. We will discuss this in detail later.

Theory: how it works internally

To understand how string methods work, you need to look under the hood of C#.

The string data type

In C#, a string is a reference type. This means that the variable does not store the text itself, but a reference to the memory area where this text is located. When you write:

string name = "Anna";

…an object is created in memory that contains the characters 'A', 'n', 'n', 'a'. The variable name stores the address of this object.

Immutability

This is a key concept. When you do:

string text = "Hello";
text = text.ToUpper(); // "HELLO"

…the string "Hello" did not change. Instead, a new string "HELLO" was created in memory, and the variable text now refers to it. The old string "Hello" remains in memory and will be removed by the garbage collector later.

Why is this done? Immutability makes strings safe for multithreading and allows memory optimization. For example, if you have 10 variables containing the word "Hello", they can all refer to the same object in memory, rather than creating 10 copies.

Characters and indices

Each character in a string has its own number — an index. Indexing starts from zero:

CharacterHello
Index01234

The length of the string "Hello" is 5. The last character always has the index length - 1.

Encoding

Internally, C# strings are stored in UTF-16 encoding. This means each character takes up 2 bytes (or 4 for rare characters). For Russian letters, English letters, numbers — everything works transparently. However, if you work with files or the network, you need to remember about encodings (UTF-8, ASCII, etc.).

Syntax in C# (.NET): all forms and variants

String methods are divided into several categories. We will look at the most important ones.

1. Creating strings

Strings can be created in different ways:

string s1 = "Simple string";                    // Most common way
string s2 = "String with \"quotes\"";            // Escaping quotes
string s3 = @"String with \ no escaping";        // Verbatim string (raw)
string s4 = $"Value: {42}";                      // Interpolation (inserting values)
string s5 = new string('*', 10);                 // String of 10 asterisks: "**********"
string s6 = new string(new char[] {'A', 'B', 'C'}); // From a character array

2. Basic methods (reference table)

```
Method Description Example Result
Length Property, returns the length of the string "Hello".Length 5
ToUpper() Converts to uppercase "Hello".ToUpper() "HELLO"
ToLower() Converts to lowercase "Hello".ToLower() "hello"
Trim() Removes whitespace from the beginning and end " Hello ".Trim() "Hello"
TrimStart() Removes whitespace only from the beginning " Hello".TrimStart() "Hello"
TrimEnd() Removes whitespace only from the end "Hello ".TrimEnd() "Hello"
Substring(int startIndex) Extracts a substring from the specified index to the end "Hello".Substring(2) "llo"
Substring(int startIndex, int length) Extracts a substring of a specified length "Hello".Substring(2, 2) "ll"
IndexOf(string value) Finds the index of the first occurrence of a substring "Hello world".IndexOf("world") 6
LastIndexOf(string value) Finds the index of the last occurrence