What is a Separator Character?
A separator character (often abbreviated "sep" in variable names) is a text character used to split a text into multiple pieces of text data.
Examples
A good example are filepaths. A filepath is a text code where filenames of folders and files are separated by forward slashes (/
) on Linux and macOS, and by backward slashes ( \
) on Windows. This is also called the path separator or directory separator.
If we have a filepath like C:\photos\vacation\2024\beach\001.jpg
or /photos/vacation/2024/beach/001.jpg
, and we split this into parts using the directory separator, we get the filenames photos
, vacation
, 2024
, beach
, and 001.jpg
. The first four are folder names, the last one is a file's filename. On Windows, C:
indicates the drive letter, not a filename, so we can ignore it.
Another example are CSV files. CSV stands for Comma-Separated Values. A CSV file encodes a table or spreadsheet as a plain text file. Columns are separated by commas (,
) or tab characters. Rows are separated by the newline character. For example, a CSV file could look like this:
Operating system,Path separator
Windows,\
macOS,/
Linux,/
Another example are Fully-Qualified Domain Names. Each level in a FQDN is separated by a dot (.
). For example, in www.virtualcuriosities.com
, com
is the top-level domain (TLD), virtualcuriosities
is the second-level domain, and www
is the subdomain.
In an IPv4 address, the dot separates each byte of the four-byte address, e.g. 127.0.0.1
. Each decimal number can only go from 0 to 255. In IPv6, there are more bytes per address, and a colon character (:
) is used to separate four hexadecimal digits that together represent 16 bits, e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
.
Solo Separators
A separator doesn't need to appear multiple times in a piece of text. Sometimes, a separator merely separates the left side from the right side.
For example, in localhost:8080
, the colon (:
) separates the hostname (localhost
) from the port (8080
).
In key-value assignments, the equal sign (=
) or colon (:
) may separate the variable name from its value. For example, in Bash:
MY_ENV_VAR=123
In JSON:
{
"foo": 1,
"bar": 2,
}
Counter-Examples
A character is only a separator if it separates the same type of data, or separates members of an ordered tuple where each position has different purpose.
A character that groups a piece of data, e.g. quotes ("
, '
), parentheses ((
, )
), brackets ([
, ]
), and braces ({
, }
), is called a delimiter instead.
Some characters may have special purpose and not be delimiters or separators, but simply parsed as part of some more complex syntax. For example, a simple math expression like 1 + 2 * 4
doesn't contain separators. More technically, with a separator we can simply look for a single separating character in a text to split it. But with a math expression, we need to look at each character, one by one, to see if it represents a mathematical operation like addition (+
), subtraction (-
), multiplication (*
), or division (/
).
Usage in Programming
Most programming languages have standard functions to split text strings by a separator in lists of data and to join those lists back using a separator.
For example, in Python we can use list.split(sep)
and string.join(list)
:
as_string = 'foo,bar,fish,fries'
as_list = ['foo', 'bar', 'fish', 'fries']
assert as_string.split(',') == as_list
assert ','.join(as_list) == as_string
In Javascript:
const as_string = 'foo,bar,fish,fries';
const as_array = ['foo', 'bar', 'fish', 'fries'];
console.assert(same_array(as_string.split(','), as_array));
console.assert(as_array.join(',') == as_string);
function same_array(a, b) {
if(a.length !== b.length) {
return false;
}
for(let i = 0; i < a.length; i++) {
if(a[i] !== b[i]) {
return false;
}
}
return true;
}
As you can see above, even though Javascript doesn't come with a built-in function to check for array equality like Python does so we need to write our own, it does come with functions to split and join strings.
Leave a Reply