Programming Fundamentals/Strings

From Wikibooks, open books for an open world
Jump to navigation Jump to search


A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. A variable may allow its elements to be mutated and the length changed, or it may be fixed (after creation). A string is generally considered a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding.[1]


Depending on the programming language and data type used, a variable declared to be a string may either cause storage in memory to be statically allocated for a predetermined maximum length or employ dynamic allocation to allow it to hold any number of elements a person wishes. String data types were also covered in chapter two of this textbook, under "String Data Types". When a string appears literally in the source code, it is known as a string literal or an anonymous string.[2]

Most data is more complex than just one character, integer, etc.  Programming languages develop other methods to represent and store data that are more complex. A complex data type of array is the first most students encounter. An array is a sequenced collection of elements of the same data type with a single identifier name. This definition perfectly describes our string data type concept. The simplest array is called a one-dimensional array; also know as a list because we usually list the members or elements vertically. However, strings are viewed as a one-dimensional array that is displayed horizontally.  Strings are an array of character data.

In the “C” programming language, all strings were handled as an array of characters that ended in an ASCII null character (the value 0 or the first character in the ASCII character code set). This approach required programmers to manually process string length and manage string storage. Buffer overflows were common. A buffer overflow, or buffer overrun, is an anomaly where a program, while writing data to a buffer, overruns the buffer’s boundary and overwrites adjacent memory locations.[3]

Most current programming languages implement strings as a data type or class where strings are stored as a length controlled array. String length and storage are handled by the compiler or interpreter, reducing program errors.

Language Reserved Word
C++ string
C# String
Java String
JavaScript String
Python str()
Swift String

Key Terms[edit]

A sequenced collection of elements of the same data type with a single identifier name.
buffer overflow
An anomaly where a program overruns a memory storage location and overwrites adjacent memory locations.
Combining two strings into one string.
An array of single digits or letters that is typically used to display to the user or is the user's input.
string class
A complex data item that uses object oriented programming.