Programming Fundamentals/Strings

Overview

edit

A string is a sequence of characters, as a literal constant or variable. A variable may be fixed or dynamic. A string is considered a data type which is implemented as an array data structure of bytes that stores a sequence of elements using character encoding. [1]

Discussion

edit

A variable declared to be a string may either cause storage in memory to be statically or dynamically allocated. 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. An array is a sequenced collection of elements of the same data type with a single identifier name. A one-dimensional array(list) lists elements vertically. Strings are viewed as a one-dimensional array that is displayed horizontally.

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]

The "Javascript" programming language JavaScript counts positions from zero. 0 is the first position in a string, 1 is the second, 2 is the third and so on. There are many string functions most important being lastIndexOf(). This searches and (if found) returns the index number of the searched character or substring within the string. Searches the string from end to the beginning. If not found, -1 is returned. “Start” is an optional argument specifying the position within string to begin the search. Default is string.length -1. Slice(start, [end]) returns a substring of the string based on the “start” and “end” index arguments, NOT including the “end” index itself. “End” is optional, and if none is specified, the slice includes all characters from “start” to the end of the string. Split(delimiter, [limit]) Splits a string into many according to the specified delimiter and returns an array containing each element. The optional “limit” is an integer that lets you specify the maximum number of elements to return. There are many more functions that are just as important but these are just an example of what string functions can do in Javascript.

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
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
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.
empty string
It is a unique string of length zero

References

edit