ligo/gitlab-pages/docs/language-basics/strings.md

100 lines
2.0 KiB
Markdown
Raw Normal View History

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