# Characters and strings


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

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:

``` bpl
⍴'a'               ⍝ ⍬ₓ
⍴"a"               ⍝ [1]ₓ
⍴"abc"             ⍝ [3]ₓ
⍴""                ⍝ [0]ₓ
```

## Quotes inside text

The character literal for a single quote is `'''`. A string can hold an
apostrophe as it is. To put a double quote inside a string, write it
twice:

``` bpl
•ucs '''           ⍝ 39ₓ
≢"can't"           ⍝ 5ₓ
≢"say ""hi"""      ⍝ 8ₓ
```

## Text across lines

A string can span lines. Every line ending inside it reads as one
newline, and its indentation stays as written. The REPL waits for the
closing quote.

``` bpl
poem←"Roses are red,
  violets are blue"
≢poem
poem
```

## Strands of text

Literals separated by spaces form one vector, called a strand, whether
they are characters or strings:

``` bpl
'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](keyed.ipynb) covers keyed arrays.

## How text displays

On its own, a string displays 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:

``` text
      "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. Reverse, for one:

``` bpl
⌽"abc"
```

    cba

`=` compares two strings one character at a time. `≡` compares them
whole:

``` bpl
"abc"="abd"
"abc"≡"abd"
```

    $t $t $f

    $f

Index of, dyadic `⍳`, finds where each character first appears. A
missing character gets the length of the string:

``` bpl
"abc"⍳"cz"
```

    [2 3]ₓ

Find, `⍷`, marks each position where a pattern starts:

``` bpl
"an"⍷"banana"
```

    $f $t $f $t $f $f

Grade orders characters by code point. Selecting with the grade sorts
the string:

``` bpl
[⍋"bca"]⌷"bca"
```

    abc

Adding an integer to a character moves it along the Unicode code points.
Subtracting one character from another gives the distance between them:

``` bpl
'a'+3
'a'+⍳3
'd'-'a'
```

    'd'

    abc

    3ₓ

The order comparisons, such as `<`, take only numbers. Grade is the way
to sort text.

[System functions](system-functions.qmd) cover the rest of text
handling. `•ucs` converts between characters and code points:

``` bpl
•ucs "ABC"
```

    [65 66 67]ₓ

`•c` changes case:

``` bpl
•c "Hello"
```

    hello

`$a` and `$d` hold the alphabet and the digits. Membership, `∊`, then
tests which characters are digits:

``` bpl
"a2b"∊$d
```

    $f $t $f

`⍕` formats any value as text:

``` bpl
"Total: ",⍕+/1.5 2
```

    Total: 3.5

[Regular expressions](regex.ipynb) covers matching patterns in text with
`•r`.
