Character string
In programming, a character string, words, character string, or phrase ( string, in English) is an ordered sequence (of arbitrary, though finite length) of elements belonging to a certain formal language or alphabet analogous to a formula or a sentence. In general, a character string is a sequence of characters (letters, numbers, or other signs or symbols). If no restrictions are placed on the alphabet, a string may consist of any finite combination of the available characters (the letters 'a' through 'z' and 'A' to 'Z', the numbers '0' to '9', the blank space '', various symbols '!', '@', '%', etc.).
In this same scope, they are commonly used as a predefined data type, for words, phrases or any other sequence of characters. In this case, they are stored in a data vector, or single-row data array (array). Strings can be stored physically:
- Followed.
- Linked letter to letter.
Characters are usually stored one after the other for access efficiency.
A special string case is the one that contains zero characters. This chain is called an empty chain; in automaton theory, it is common to represent it through the Greek letter ε ε {displaystyle epsilon }.
String operation
When considering strings as a data type, one must define what operations can be done on them. In principle, there could be many and become very sophisticated. The following are some of them:
- Assignment: It consists of assigning one string to another.
- Concatenation: It consists of joining two chains or more (or a string with one character) to form a larger chain.
- Search: It consists in locating within a chain a smaller subchain or a character.
- Extraction: It is about removing a portion of it out of a chain according to its position within it.
- Comparison: Used to compare two strings.
Representation
A string is usually represented in upper double quotes ("word"), while a character in that string (an English char) is usually represented in single quotes ('p'). For example, in C:
charat c = 'a'; char str[5] = "hello";
Generally, to access a character at a certain position, the form variable[position] is used as when accessing a vector.
In order to display a quote (") inside the string and not have problems with the surrounding quotes, we use escape sequences. This applies to other reserved or non-printable characters like carriage returns. However, the expressions to produce these escape sequences depend on the programming language being used. A common way, in many languages, to escape a character is by preceding it with a "" (without quotes), e.g. e.: «"» (without quotation marks).
Dynamic and static strings
Strings can be dynamic in nature (they can change their length at runtime) or static in nature (their length is fixed throughout runtime). In this second case, the programmer must anticipate that when traversing the string the indices will not go outside the expected limits (C does not allow strings to grow automatically automatically, while C# does).
The end of the string is delimited differently in either case:
- By means of a chain end character ('' in C) for dynamic chains.
- Using a chain property that delimits its length (
lengthin C#) for static type.
Examples of Common Operations
Assignment
Assign one string to another.
char *strcpy(char [], const char[]); # in C chain1 = chain2; # in C++ chain1:= chain2 # in Smalltalk
Concatenation
Join two strings.
couple = "Joshua" + and " + "Marina" # in Python $pareja = "Joshua" and "Marina" # in Perl and PHP couple = "Luisa" and "Carmen" # in Visual Basic pair = "Luisa" + " and " + "Carmen"; # in C++ and Java with String class couple:= 'Luisa' and 'Carmen'. # in Smalltalk strcat(cadena3,cadena1); strcat(chain2, chain1); # in C (There must be enough space in the first.)
Number of characters in a string
int strlen(const char[]); # in C Returns the number of characters without counting the '' string.length(); # in C++ chain size. # in Smalltalk in Python
Comparison
Compare two strings in lexicographical order.
int strcmp(const char[], const char[]); # in C Devolv ≥0 if the 1.a is minor, 0 if it is greater and 0 if they are equal. chain1 == chain2; chain1 censorship2; etc. # in C++ Returns a true value. chain1 = chain2 # in Smalltalk Returns true or false.
Multiply a string
Repeat a chain several times.
$points ="" x 5 # puts 5 points in Perl. points:= Generator generateJoin: '.' repeat: 5. # Generates a 5-point chain in Smalltalk. points = "." * 5 # Generates a 5-point chain on Python.
Contenido relacionado
Bit rate
Smarty
Cyc