String (computer Science) Article Index for
String
Website Links For
String
 

Information About

String (computer Science)




In programming, when stored in memory each symbol is represented using a numeric value. A Variable declared to have a string Datatype usually causes storage to be allocated in memory that is capable of holding some predetermined number of symbols. When it appears in Source Code a string is known as a String Literal and has a representation that denotes it as such. Sometimes the term binary string is used to refer to an arbitrary sequence of bits.


FORMAL THEORY

Let Σ 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 Σ.

  • . In terms of Σ''n'', \Sigma^{---} = \bigcup_{n=0}^{\infty}\Sigma^{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.


  • ) 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 substrings

  • . 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.


  • 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.




Lexicographical ordering

  • 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 operations

A number of additional operations on strings commonly occur in the formal theory. These are given in the article on String Operations .


STRING DATATYPES

A 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 Type s and in others as Composite Type s. 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 length

Although 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 encoding

Historically, string datatypes allocated one Byte per character, and although the exact character set varied by region, Character Encoding s were similar enough that programmers could generally get away with ignoring this — groups of character sets used by the same system in different regions 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 .

Logograph ic languages such as Chinese , Japanese , and Korean (known collectively as CJK ) need far more than 256 characters (the limit of a one- 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 complicated 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.


Implementations

Some 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.


Representations

Representations 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 Array s with the entries storing the Character Code s 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
  Last Bryant
  First Randal E
  Author-link Randal Bryant
  Last2 David
  First2 O'Hallaron
  Title Computer Systems: A Programmer's Perspective
  Place Upper Saddle River, NJ
  Publisher Pearson Education
  Year 2003
  Edition 2003
  Url http://csappcscmuedu/
  Isbn 0-13-034074-X


  { Class "wikitable" style="margin-left: auto margin-right:auto"
  { Class "wikitable" style="margin-left: auto margin-right:auto"