Brace notation

Faster way to extract bytes from a string variable

This article does not cite any sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Brace notation" – news · newspapers · books · scholar · JSTOR (December 2009) (Learn how and when to remove this message)

|

In several programming languages, such as Perl, brace notation is a faster way to extract bytes from a string variable.

## In pseudocode

[edit]

An example of brace notation using pseudocode which would extract the 82nd character from the string is:

a_byte = a_string{82}

The equivalent of this using a hypothetical function 'MID' is:

a_byte = MID(a_string, 82, 1)

## In C

[edit]

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 <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
char* a_string = "Test";
printf("%c", a_string[0]); // Would print "T"
printf("%c", a_string[1]); // Would print "e"
printf("%c", a_string[2]); // Would print "s"
printf("%c", a_string[3]); // Would print "t"
printf("%c", a_string[4]); // Would print the 'null' character (ASCII 0) for end of string
return(0);

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#

[edit]

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[0];
char e = var[1];
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

[edit]

In Python, strings are immutable, so it's hard to modify an existing string, but it's easy to extract and concatenate strings to each other: Extracting characters is even easier:

>>> var = 'hello world'
>>> var[0]  # Return the first character as a single-letter string
'h'
>>> var[-1]
'd'
>>> var[len(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[8] + var[7] + var[2] + var[1]
>>> 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[0: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[0:len(var):2]
'acegikm'

## In PHP

[edit]

PHP 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[0] . $a[1] . $a[8] . $a[3] . $a[6];
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

[edit]

JavaScript brace notation works the same as in C# and PHP.

var myString = "Hello" + "World";
alert(myString[0] + " " + myString[5]); // alerts the message: H W

## In MATLAB

[edit]

MATLAB handles brace notation slightly differently from most common programming languages.

>> var = 'Hello World'

var =

Hello World

>> var(1)

ans =

Strings begin with index 1 enclosed in parentheses, 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 [ ] is reserved for creating matrices in MATLAB.

## See also

[edit]

* Array slicing

## References

[edit]