String

From Rosetta Code

In computer programming, a string is a finite sequence of characters or symbols. Most programming languages handle them in a similar way, and have language constructs for creating, modifying, and displaying them.

How strings are stored in memory

Strings are encoded using one or more encoding methods, such as ASCII or Unicode, a scheme that maps a set of glyphs to specific numeric values. There are standardized encoding methods to support portability and interoperability.

For example, this is the encoding of "Hello World" in ASCII:

0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64

Compilers for languages such as C add an extra "null" byte (0x00) at the end of the string when the string is stored in computer memory. This byte is called the null terminator and is added to make it easy for the computer to determine the end of the string. This leads to a slight difference between the representation of a string in your source code (typically with quotation marks and an invisible null terminator) and the representation in computer memory. Not every language uses a null terminator; for example, Pascal stores the length of the string before the string itself.

Why even need a null terminator or a length parameter at all? Without one, the computer doesn't have any idea where your string ends and unrelated data stored after it begins. The CPU only sees numbers, and is being told to interpret them as text. So it needs to be told when to quit. Otherwise, it would keep reading adjacent memory indefinitely (depending on how the machine code handles looping, some languages' looping constructs auto-terminate after a certain number of repetitions.)

Control codes

The first 32 characters of ASCII and Unicode are reserved for control codes. Most of these are relics of the old teletype days and many are no longer of use to most computers today, but a few of them are still used widely (like 0 for the null terminator, 8 for backspace, etc.) Most of them are nowhere to be found on your keyboard; they are used internally by the computer as a signal to perform certain tasks, or to mark the beginning or end of various data.

Escape character

An escape character is a distinguished character used to signal that the following character in a string is to be interpreted in a special way. Without escape characters, we'd run into a problem if we wanted quotation marks to appear in our string when printed to the screen, since quotation marks often mark the beginning and end of the string literal. The same is true if we wanted to have a string that happened to include the comment character in it. In many languages, the backslash \ is the escape character, but this varies depending on the language.

Escape characters are also used to encode special instructions that otherwise the user would have an extremely difficult time supplying to the computer. In C and many other languages, the two character sequence \n represents the control code for a new line. Thus if you wanted to actually print the string "\n" as-is and not just output a new line, you would need to have an additional escape character in front of it, like this: \\n.

Substitution characters

This is a similar concept to escape characters, but instead allows you to print a variable value, such as a number or the result of some calculation. The syntax for doing so will vary depending on your programming language, but here's an example from C:

printf("The sum of A plus B is %d", sum(a+b));

Here, % is the substitution character, and d tells the computer to substitute with a decimal value. The expression after the comma is what will be replacing the %d. Nearly all languages with a built-in print function will handle conversion of numeric data to text characters for you. Be thankful for this, as it's quite a difficult task — just ask any assembly programmer.