2019-11-08 03:19:27 +04:00
|
|
|
---
|
|
|
|
id: strings
|
|
|
|
title: Strings
|
|
|
|
---
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
import Syntax from '@theme/Syntax';
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
Strings are defined using the built-in `string` type like this:
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-02-05 19:28:40 +04:00
|
|
|
const a : string = "Hello Alice"
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : string = "Hello Alice"
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2019-12-11 17:47:52 +04:00
|
|
|
```reasonligo
|
2020-02-05 19:28:40 +04:00
|
|
|
let a : string = "Hello Alice";
|
2019-12-11 13:34:08 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
|
2020-02-10 22:07:20 +04:00
|
|
|
## Concatenating Strings
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2019-12-11 13:34:08 +04:00
|
|
|
Strings can be concatenated using the `^` operator.
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```pascaligo group=a
|
2020-02-05 19:28:40 +04:00
|
|
|
const name : string = "Alice"
|
|
|
|
const greeting : string = "Hello"
|
|
|
|
const full_greeting : string = greeting ^ " " ^ name
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2019-12-11 13:34:08 +04:00
|
|
|
Strings can be concatenated using the `^` operator.
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```cameligo group=a
|
2020-02-05 19:28:40 +04:00
|
|
|
let name : string = "Alice"
|
|
|
|
let greeting : string = "Hello"
|
|
|
|
let full_greeting : string = greeting ^ " " ^ name
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2019-12-11 13:34:08 +04:00
|
|
|
Strings can be concatenated using the `++` operator.
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```reasonligo group=a
|
2020-02-05 19:28:40 +04:00
|
|
|
let name : string = "Alice";
|
|
|
|
let greeting : string = "Hello";
|
|
|
|
let full_greeting : string = greeting ++ " " ++ name;
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
|
2020-02-10 22:07:20 +04:00
|
|
|
## Slicing Strings
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Strings can be sliced using a built-in function:
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```pascaligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
const name : string = "Alice"
|
2020-02-25 21:07:53 +04:00
|
|
|
const slice : string = String.slice (0n, 1n, name)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
> Note that `string_slide` is *deprecated*.
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```cameligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
let name : string = "Alice"
|
|
|
|
let slice : string = String.slice 0n 1n name
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```reasonligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
let name : string = "Alice";
|
|
|
|
let slice : string = String.slice (0n, 1n, name);
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
> ⚠️ Notice that the offset and length of the slice are natural
|
|
|
|
> numbers.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-10 22:07:20 +04:00
|
|
|
## Length of Strings
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
The length of a string can be found using a built-in function:
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```pascaligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
const name : string = "Alice"
|
2020-02-25 21:07:53 +04:00
|
|
|
const length : nat = String.length (name) // length = 5
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
> Note that `size` is *deprecated*.
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```cameligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
let name : string = "Alice"
|
2020-02-10 22:07:20 +04:00
|
|
|
let length : nat = String.size name // length = 5
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-01-09 00:58:26 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-12 01:29:12 +04:00
|
|
|
```reasonligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
let name : string = "Alice";
|
2020-02-10 22:07:20 +04:00
|
|
|
let length : nat = String.size (name); // length == 5
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|