# Strings

This page: https://emojicode-ide.com/en/docs/basics/strings/

You write a string between two `🔤`. Its type is `🔡`.

## Interpolating values (🧲)

Inside a string, whatever you put between `🧲` and `🧲` is replaced by the value of a variable or expression.

```emojicode
🏁 🍇
  🔤Emoji🔤 ➡️ name
  😀 🔤Hello, 🧲name🧲!🔤❗️
  😀 🔤1 ➕ 2 is 🧲1 ➕ 2🧲🔤❗️
  😀 🔤Half is 🧲1.5🧲🔤❗️
🍉
```

Output:

```text
Hello, Emoji!
1 ➕ 2 is 3
Half is 1.500000
```

What goes inside `🧲` must be a value of a type that can be turned into a string. Strings, integers, and real numbers work, but Booleans, lists, and optionals can't go in as they are. Real numbers are shown with 6 decimal places, so if you want to choose the number of digits, turn the number into a string first with `🔡 real digits❗️` from “Converting to and from numbers” below.

Example of a compile error:

```emojicode
🏁 🍇
  🔢 🔤12🔤 10❗️ ➡️ n
  😀 🔤n is 🧲n🧲🔤❗️
🍉
```

Error:

```text
is not compatible to
```

Unwrap an optional before putting it in ([Optionals](https://emojicode-ide.com/en/docs/basics/optionals.md)). Your own types can go inside `🧲` too, if they provide `❗️ 🔡 ➡️ 🔡` and conform to the protocol `↘️🔸🔡` ([Protocols](https://emojicode-ide.com/en/docs/types/protocols.md)).

## Special characters (❌)

To write a newline, a tab, `🔤` itself, and so on inside a string, put `❌` in front of it.

| Syntax | Meaning |
|---|---|
| `❌n` | Newline |
| `❌t` | Tab |
| `❌🔤` | The character `🔤` |
| `❌🧲` | The character `🧲` |
| `❌❌` | The character `❌` |

```emojicode
🏁 🍇
  😀 🔤Line 1❌nLine 2🔤❗️
  😀 🔤❌🔤 for strings, ❌🧲 for interpolation🔤❗️
🍉
```

Output:

```text
Line 1
Line 2
🔤 for strings, 🧲 for interpolation
```

## Converting to and from numbers

### Number → string

`🔡 number❗️` turns an integer into a decimal string. A second argument lets you choose the base (radix).

```emojicode
🏁 🍇
  😀 🔡 255❗️❗️
  😀 🔡 255 16❗️❗️
  😀 🔡 255 2❗️❗️
🍉
```

Output:

```text
255
ff
11111111
```

For a real number, `🔡 number digits❗️` lets you choose how many digits after the decimal point to output.

```emojicode
🏁 🍇
  😀 🔡 2.5 1❗️❗️
  😀 🔡 2.5 3❗️❗️
🍉
```

Output:

```text
2.5
2.500
```

> When a real number is turned into a string, digits beyond the ones you asked for are **truncated, not rounded** (`🔡 3.05 2❗️` can even become `3.04`). This is a bug in the official compiler. See [Emojicode Language Bugs](https://emojicode-ide.com/en/docs/about/known-issues.md) for details.

### String → number

`🔢 string base❗️` turns a string into an integer.

A string can't always be read as a number, so the result is `🍬🔢` (an optional), which means “an integer, or no value.” To unwrap it, use `↪️ … ➡️ name`.

```emojicode
🏁 🍇
  ↪️ 🔢 🔤42🔤 10❗️ ➡️ n 🍇
    😀 🔡 n ➕ 1❗️❗️
  🍉
  ↪️ 🔢 🔤forty-two🔤 10❗️ ➡️ n 🍇
    😀 🔤Never gets here🔤❗️
  🍉
  🙅 🍇
    😀 🔤Not a number🔤❗️
  🍉
🍉
```

Output:

```text
43
Not a number
```

`↪️` and `🙅` are conditionals (if and else). Optionals are covered in detail in a later chapter.

## Common methods

| Syntax | Result |
|---|---|
| `📫 string❗️` | Convert to uppercase |
| `📪 string❗️` | Convert to lowercase |
| `🔪 string start length❗️` | Take out part of it (position and length count visible characters) |
| `🔧 string❗️` | Remove whitespace at both ends |
| `🔫 string separator❗️` | Split at the separator into a list (`🍨🐚🔡🍆`) |
| `🔍 string search❗️` | The position where it was found (`🍬🔢`) |
| `🎼 string prefix❗️` / `⛳️ string suffix❗️` | Whether it starts / ends with that string (`👌`) |

```emojicode
🏁 🍇
  😀 📫 🔤emojicode🔤❗️❗️
  😀 🔪 🔤🍎🍌🍇🍑🔤 1 2❗️❗️
  😀 🔧 🔤  surrounding spaces  🔤❗️❗️
  ↪️ 🎼 🔤🍇🍉🔤 🔤🍇🔤❗️ 🍇
    😀 🔤Starts with 🍇🔤❗️
  🍉
🍉
```

Output:

```text
EMOJICODE
🍌🍇
surrounding spaces
Starts with 🍇
```

Positions in `🔪` count from 0. It takes 2 characters starting at position 1 of `🍎🍌🍇🍑`, so the result is `🍌🍇`. Even if an emoji is made of several code points, it counts as one visible character.

> `📐 string❗️` returns the number of **bytes** in UTF-8, not the number of characters. `📐 🔤えもじ🔤❗️` is 9.

## Comparing strings

`🙌` checks whether two strings are the same.

```emojicode
🏁 🍇
  🔤abc🔤 ➡️ a
  ↪️ a 🙌 🔤abc🔤 🍇
    😀 🔤Same🔤❗️
  🍉
🍉
```

Output:

```text
Same
```
