String (computer science)
Encyclopedia
|
| Tutorials | Encyclopedia | Dictionary | Directory |
|
String (computer science)
In computer programming and some branches of mathematics, a string is an ordered sequence of symbols. These symbols are chosen from a predetermined set or alphabet. In computer programming, a string is generally understood as a data type storing a sequence of data values, usually bytes, in which elements usually stand for characters according to a character encoding, which differentiates it from the more general array data type. In this context, the terms binary string and byte string are used to suggest strings in which the stored data does not (necessarily) represent text. A variable declared to have a string data type usually causes storage to be allocated in memory that is capable of holding some predetermined number of symbols. When a string appears literally in source code, it is known as a string literal and has a representation that denotes it as such.
Formal theoryLet ? be an alphabet, a non-empty finite set. Elements of ? are called symbols or characters. A string (or word) over ? is any finite sequence of characters from ?. For example, if ? = {0, 1}, then 0101 is a string over ?. The length of a string is the number of characters in the string (the length of the sequence) and can be any non-negative integer. The empty string is the unique string over ? of length 0, and is denoted ? or ?. The set of all strings over ? of length n is denoted ?n. For example, if ? = {0, 1}, then ?2 = {00, 01, 10, 11}. Note that ?0 = {?} for any alphabet ?. The set of all strings over ? of any length is the Kleene closure of ? and is denoted ?*. In terms of ?n,
For example, if ? = {0, 1}, ?* = {?, 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, ?}. Although ?* itself is countably infinite, all elements of ?* have finite length. A set of strings over ? (i.e. any subset of ?*) is called a formal language over ?. For example, if ? = {0, 1}, the set of strings with an even number of zeros ({?, 1, 00, 11, 001, 010, 100, 111, 0000, 0011, 0101, 0110, 1001, 1010, 1100, 1111, ?}) is a formal language over ?. Concatenation and substringsConcatenation is an important binary operation on ?*. For any two strings s and t in ?*, their concatenation is defined as the sequence of characters in s followed by the sequence of characters in t, and is denoted st. For example, if ? = {a, b, ?, z}, s = bear, and t = hug, then st = bearhug and ts = hugbear. String concatenation is an associative, but non-commutative operation. The empty string serves as the identity element; for any string s, ?s = s? = s. Therefore, the set ?* and the concatenation operation form a monoid, the free monoid generated by ?. In addition, the length function defines a monoid homomorphism from ?* to the non-negative integers. A string s is said to be a substring or factor of t if there exist (possibly empty) strings u and v such that t = usv. The relation "is a substring of" defines a partial order on ?*, the least element of which is the empty string. Lexicographical orderingIt is often necessary to define an ordering on the set of strings. If the alphabet ? has a total order (cf. alphabetical order) one can define a total order on ?* called lexicographical order. Note that since ? is finite, it is always possible to define a well ordering on ? and thus on ?*. For example, if ? = {0, 1} and 0 < 1, then the lexicographical ordering of ?* is ? < 0 < 00 < 000 < ? < 011 < 0110 < ? < 01111 < ? < 1 < 10 < 100 < ? < 101 < ? < 111 ? String operationsA number of additional operations on strings commonly occur in the formal theory. These are given in the article on string operations. TopologyStrings admit the following interpretation as nodes on a graph:
The natural topology on the set of fixed length strings or variable length strings is the discrete topology, but the natural topology on the set of infinite strings is the limit topology, viewing the set of infinite strings as the inverse limit of the sets of finite strings. This is the construction used for the p-adic numbers and some constructions of the Cantor set, and yields the same topology. String datatypesA string datatype is a datatype modeled on the idea of a formal string. Strings are such an important and useful datatype that they are implemented in nearly every programming language. In some languages they are available as primitive types and in others as composite types. The syntax of most high-level programming languages allows for a string, usually quoted in some way, to represent an instance of a string datatype; such a meta-string is called a literal or string literal. String lengthAlthough formal strings can have an arbitrary (but finite) length, the length of strings in real languages is often constrained to an artificial maximum. In general, there are two types of string datatypes: fixed length strings which have a fixed maximum length and which use the same amount of memory whether this maximum is reached or not, and variable length strings whose length is not arbitrarily fixed and which use varying amounts of memory depending on their actual size. Most strings in modern programming languages are variable length strings. Despite the name, even variable length strings are limited in length; although, generally, the limit depends only on the amount of memory available. Character encodingHistorically, string datatypes allocated one byte per character, and although the exact character set varied by region, character encodings were similar enough that programmers could generally get away with ignoring this — groups of character sets used by the same system in different regions usually either had a character in the same place, or did not have it at all. These character sets were typically based on ASCII or EBCDIC. Logographic languages such as Chinese, Japanese, and Korean (known collectively as CJK) need far more than 256 characters (the limit of a one 8-bit byte per-character encoding) for reasonable representation. The normal solutions involved keeping single-byte representations for ASCII and using two-byte representations for CJK ideographs. Use of these with existing code led to problems with matching and cutting of strings, the severity of which depended on how the character encoding was designed. Some encodings such as the EUC family guarantee that a byte value in the ASCII range will only represent that ASCII character, making the encoding safe for systems that use those characters as field separators. Other encodings such as ISO-2022 and Shift-JIS do not make such guarantees, making matching on byte codes unsafe. Another issue is that if the beginning of a string is deleted, important instructions for the decoder or information on position in a multibyte sequence may be lost. Another is that if strings are joined together (especially after having their ends truncated by code not aware of the encoding), the first string may not leave the encoder in a state suitable for dealing with the second string. Unicode has simplified the picture somewhat. Most languages have a datatype for Unicode strings (usually UTF-16 as it was usually added before Unicode supplemental planes were introduced). Converting between Unicode and local encodings requires an understanding of the local encoding, which may be problematic for existing systems where strings of various encodings are being transmitted together with no real marking as to what encoding they are in. ImplementationsSome languages like C++ implement strings as templates that can be used with any primitive type, but this is the exception, not the rule. If an object-oriented language represents strings as objects, they are called mutable if the value can change at runtime and immutable if the value is frozen after creation. For example, Ruby has mutable strings, while Python's strings are immutable. Other languages, most notably Prolog and Erlang, avoid implementing a string datatype, instead adopting the convention of representing strings as lists of character codes. RepresentationsRepresentations of strings depend heavily on the choice of character repertoire and the method of character encoding. Older string implementations were designed to work with repertoire and encoding defined by ASCII, or more recent extensions like the ISO 8859 series. Modern implementations often use the extensive repertoire defined by Unicode along with a variety of complex encodings such as UTF-8 and UTF-16. Most string implementations are very similar to variable-length arrays with the entries storing the character codes of corresponding characters. The principal difference is that, with certain encodings, a single logical character may take up more than one entry in the array. This happens for example with UTF-8, where single characters can take anywhere from one to four bytes. In these cases, the logical length of the string differs from the logical length of the array. The length of a string can be stored implicitly by using a special terminating character; often this is the null character having value zero, a convention used and perpetuated by the popular C programming language[1]. Hence, this representation is commonly referred to as C string. The length of a string can also be stored explicitly, for example by prefixing the string with the length as a byte value — a convention used in Pascal; consequently some people call it a P-string. In terminated strings, the terminating code is not an allowable character in any string. The term bytestring usually indicates to strings of bytes — rather than bits or the wider concept of characters, which may take more space than a byte — that are not terminated in this way, and in bytes may take any value. Here is an example of a null-terminated string stored in a 10-byte buffer, along with its ASCII representation:
Source: Wikipedia | The above article is available under the GNU FDL. | Edit this article
|
|
top
©2008-2009 TutorGig.com. All Rights Reserved. Privacy Statement