Search⌘ K
AI Features

Introduction to Strings

Explore the core concepts of strings as ordered sequences of characters in C#. Understand how strings differ from arrays, how they are stored and represented internally, and why order and encoding matter. Gain foundational knowledge essential for string manipulation and algorithmic problem-solving in programming.

We'll cover the following...

By this point, you are familiar with arrays as ordered collections of elements stored by position. Arrays provide a general way to store sequences of values such as integers, floating-point numbers, and objects. Each element in an array can be accessed efficiently using its index.

However, many important computational problems involve textual and symbolic data rather than only numeric values. Names, passwords, file paths, search queries, source code, and biological sequences are all examples of data that must be handled as sequences of characters. Although arrays can store characters, viewing such data only as a general array is often not enough for algorithmic study.

Character-based data gives rise to a different kind of problem. In numerical arrays, common tasks include searching for values, computing sums, or rearranging elements. In character sequences, the focus shifts to questions such as whether two words are equal, whether a pattern appears inside a larger text, how often a character occurs, whether a string is a palindrome, and how two strings compare.

Strings provide a natural way to represent this type of data. A string is an ordered sequence of characters, and it forms the basis of many important computational tasks such as text processing, pattern matching, and validation.

What is a character?

Before defining a string precisely, we need to be clear about what a string is made of. The basic building block of every string is a character.

A character is a single symbol. It can be a letter like 'a' or 'Z', a digit like '3' or '9', a space, a punctuation mark like '?' or '.', or a special symbol like '@' or '#'. Anything you can type on a keyboard is a character. Each character represents one unit of textual information, and it occupies one position in a string.

It is important to distinguish a character from a string early on because they are two different things, even though they can look similar when written in code.

char ch = 'h'; // a single character
string s = "hello"; // a string made up of 5 characters

A single character is the smallest possible unit. ...