Strings

Overview

A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter 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]

Discussion

Recall from String Data Type earlier in the book that, depending on programming language and precise 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 a variable number of elements. When a string appears literally in 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 visualize as listed horizontally.  Strings are an array of character data.

In the “C” programming language all strings were handled as an array of characters that end 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

array
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.
concatenation
Combining two strings into one string.
string class
A complex data item that uses object oriented programming.

References


License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Programming Fundamentals Copyright © 2018 by Authors and Contributors is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book