Characters and strings
Characters, strings and vectors of strings: writing them, quoting, display, and the functions that work on text
A character is an atom, like a number, written in single quotes. A string is a vector of any number of characters, even one or none, written in double quotes. '' also gives the empty string:
ρ'a' ⍝ ⍬ₓ
ρ"a" ⍝ [1]ₓ
ρ"abc" ⍝ [3]ₓ
ρ'' ⍝ [0]ₓ
Quotes inside text
The character literal for a single quote is '''. A string can contain an unescaped single quote. To put a double quote inside a string, write it twice:
•ucs ''' ⍝ 39ₓ
≢"can't" ⍝ 5ₓ
≢"say ""hi""" ⍝ 8ₓ
Text across lines
A string can span lines. Every line ending inside it becomes one newline, and its indentation stays as written. The REPL waits for the closing quote.
poem←"Roses are red,
violets are blue"
≢poem
poem
33ₓ
"Roses are red,
violets are blue"
Strands of text
Literals separated by spaces form one vector, called a strand, whether they are characters or strings:
'a' 'b' 'c' ⍝ "abc"
≢"one" "three" ⍝ 2ₓ
≢¨"one" "three" ⍝ [3 5]ₓ
Keys are written as a vector of strings, as in "x" "y":1 2. Axis keys covers keyed arrays.
How text is displayed
On its own, a string is displayed as plain text and a character matrix as lines of text. Inside a larger array, strings keep their double quotes and characters their single quotes. In the REPL:
"abc"
abc
2 3ρ"abcdef"
abc
def
⊂"ab"
⊂"ab"
"abc",⊂"de"
'a' 'b' 'c' "de"
Working with text
Functions that work on vectors work on strings too, such as ⌽:
⌽"abc"
"cba"
= compares two strings one character at a time. ≡ compares them as a whole:
"abc"="abd"
"abc"≡"abd"
$t $t $f
$f
ι finds the first position in the left string of each character in the right string. For a character not in the left string, ι gives the length of the left string:
"abc"ι"cz"
[2 3]ₓ
Find ⍷ marks each position where a pattern starts:
"an"⍷"banana"
$f $t $f $t $f $f
< sorts characters by code point:
<"bca"
"abc"
Adding a whole number to a character moves it along the Unicode code points. Subtracting one character from another gives the distance between their code points:
'a'+3
'a'+ι3ₓ
'd'-'a'
'd'
"abc"
3ₓ
The order comparisons, such as <, compare characters by code point. A number is less than any character.
System functions cover the rest of text handling. •ucs converts between characters and code points:
•ucs "ABC"
[65 66 67]ₓ
•c changes case:
•c "Hello"
"hello"
$a holds the uppercase alphabet, and $d holds the digits. ∊ tests which characters are in $d:
"a2b"∊$d
$f $t $f
⍕ formats any value as text:
"Total: ",⍕+/1.5 2
"Total: 3.5"
Regular expressions covers matching patterns in text with •r.