Brace notation
   HOME

TheInfoList



OR:

In several
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s, such as
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
, brace notation is a faster way to extract
byte The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable un ...
s from a
string String or strings may refer to: *String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects Arts, entertainment, and media Films * ''Strings'' (1991 film), a Canadian anim ...
variable.


In pseudocode

An example of brace notation using pseudocode which would extract the 82nd character from the string is: a_byte = a_string The equivalent of this using a hypothetical function 'MID' is: a_byte = MID(a_string, 82, 1)


In C

In C, strings are normally represented as a character array rather than an actual string data type. The fact a string is really an array of characters means that referring to a string would mean referring to the first element in an array. Hence in C, the following is a legitimate example of brace notation: #include #include #include int main(int argc, char* argv[]) Note that each of a_string[n] would have a 'char' data type while a_string itself would return a pointer to the first element in the a_string character array.


In C#

C# handles brace notation differently. A string is a primitive type that returns a char when encountered with brace notation: String var = "Hello World"; char h = var char e = var String hehe = h.ToString() + e.ToString(); // string "he" hehe += hehe; // string "hehe" To change the char type to a string in C#, use the method ToString(). This allows joining individual characters with the addition symbol + which acts as a concatenation symbol when dealing with strings.


In Python

In Python, strings are immutable, so it's hard to modify an existing string, but it's easy to extract and
concatenate In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalizations of concatenati ...
strings to each other: Extracting characters is even easier: >>> var = 'hello world' >>> var # Return the first character as a single-letter string 'h' >>> var 1'd' >>> var en(var)-1 # len(var) is the length of the string in var; len(var)-1 is the index of the last character of the string. 'd' >>> var = var + ' ' + var + var + var + var >>> var 'hello world role' Python is flexible when it comes to details, note var 1'' takes -1 as the index number. That index is interpreted as the first character beginning from the end of the string. Consider 0 as the index boundary for a string; zero is inclusive, hence it will return the first character. At index 1 and above, all characters belonging to each index are 'extracted' from left to right. At index -1 and below, all characters are 'extracted' from right to left. Since there are no more characters before index 0, Python "redirects" the cursor to the end of the string where characters are read right to left. If a string has length n, then the maximum index boundary is n-1 and the minimum index boundary is -n which returns the same character as index 0, namely the first character. It is also possible to extract a sequence of characters: >>> var :5'hello' Notice that the last number in the sequence is exclusive. Python extracts characters beginning at index 0 up to and excluding 5. One can also extract every x character in the sequence, in this case x=2: >>> var = 'abcdefghijklmn' >>> var :len(var):2'acegikm'


In PHP

PHP PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. ...
strings can grow very large and can use all available memory, if a large enough string occurs. Usually, if that's the case, it may be better to split() a string into an array for finer control. Brace notation in PHP looks like: $a = "Hello" . 'World'; $c = $a . $a . $a . $a . $a echo $c . " " . strlen($c); // Hello 5 Note that variable $a accepts characters inside a double quote or single quote as the same string. PHP expects the string to end with the same quotation mark as the opening quote(s). Brace notation on a string always returns a string type.


In JavaScript

JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
brace notation works the same as in C# and PHP. var myString = "Hello" + "World"; alert(myString + " " + myString ; // alerts the message: H W


In MATLAB

MATLAB MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementat ...
handles brace notation slightly differently from most common programming languages. >> var = 'Hello World' var = Hello World >> var(1) ans = H Strings begin with index 1 enclosed in
parentheses A bracket is either of two tall fore- or back-facing punctuation marks commonly used to isolate a segment of text or data from its surroundings. They come in four main pairs of shapes, as given in the box to the right, which also gives their n ...
, since they are treated as matrices. A useful trait of brace notation in MATLAB is that it supports an index range, much like Python: >> var(1:8) ans = Hello Wo >> var(1:length(var)) ans = Hello World The use of square brackets nbsp;'' is reserved for creating matrices in MATLAB.


See also

*
Array slicing In computer programming, array slicing is an operation that extracts a subset of elements from an array and packages them as another array, possibly in a different dimension from the original. Common examples of array slicing are extracting a ...


References

{{DEFAULTSORT:Brace notation Programming constructs