2019-11-08 03:19:27 +04:00
|
|
|
---
|
|
|
|
id: strings
|
|
|
|
title: Strings
|
|
|
|
---
|
|
|
|
|
|
|
|
Strings are defined using the built-in `string` type like this:
|
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
|
|
|
```
|
2020-02-05 19:28:40 +04:00
|
|
|
const a : string = "Hello Alice"
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--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
|
|
|
```
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--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
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
|
|
|
|
|
|
## Concatenating strings
|
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
2019-12-11 13:34:08 +04:00
|
|
|
Strings can be concatenated using the `^` operator.
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
```pascaligo
|
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
|
|
|
```
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-12-11 13:34:08 +04:00
|
|
|
Strings can be concatenated using the `^` operator.
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
```cameligo
|
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
|
|
|
```
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
|
|
|
Strings can be concatenated using the `++` operator.
|
|
|
|
|
2019-12-10 17:47:31 +04:00
|
|
|
```reasonligo
|
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
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
|
|
|
|
|
|
## Slicing strings
|
|
|
|
|
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
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
|
|
|
```pascaligo
|
2020-02-05 19:28:40 +04:00
|
|
|
const name : string = "Alice"
|
|
|
|
const slice : string = string_slice (0n, 1n, name)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-11-08 03:19:27 +04:00
|
|
|
```cameligo
|
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
|
|
|
```
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
2019-12-10 17:47:31 +04:00
|
|
|
```reasonligo
|
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
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
> ⚠️ Notice that the `offset` and slice `length` are natural numbers
|
|
|
|
> (`nat`).
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
## Aquiring the length of a string
|
|
|
|
|
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
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
|
|
|
```pascaligo
|
2020-02-05 19:28:40 +04:00
|
|
|
const name : string = "Alice"
|
2019-11-08 03:19:27 +04:00
|
|
|
// length = 5
|
2020-02-05 19:28:40 +04:00
|
|
|
const length : nat = size (name)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-11-08 03:19:27 +04:00
|
|
|
```cameligo
|
2020-02-05 19:28:40 +04:00
|
|
|
let name : string = "Alice"
|
|
|
|
let length : nat = String.size name
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
2020-01-09 00:58:26 +04:00
|
|
|
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
2019-12-10 17:47:31 +04:00
|
|
|
```reasonligo
|
2020-02-05 19:28:40 +04:00
|
|
|
let name : string = "Alice";
|
|
|
|
let length : nat = String.size (name);
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
2020-01-09 00:58:26 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|