ligo/gitlab-pages/docs/reference/string.md

103 lines
1.8 KiB
Markdown
Raw Normal View History

2020-02-07 17:27:23 +04:00
---
id: string-reference
title: String — Manipulate string data
2020-02-07 17:27:23 +04:00
---
import Syntax from '@theme/Syntax';
2020-02-07 17:27:23 +04:00
## String.size(s: string) : nat
Get the size of a string. [Michelson only supports ASCII strings](http://tezos.gitlab.io/whitedoc/michelson.html#constants)
so for now you can assume that each character takes one byte of storage.
<Syntax syntax="pascaligo">
2020-02-07 17:27:23 +04:00
```pascaligo
function string_size (const s: string) : nat is size(s)
```
</Syntax>
<Syntax syntax="cameligo">
2020-02-07 17:27:23 +04:00
```cameligo
let size_op (s: string) : nat = String.size s
```
</Syntax>
<Syntax syntax="reasonligo">
2020-02-07 17:27:23 +04:00
```reasonligo
let size_op = (s: string): nat => String.size(s);
```
</Syntax>
2020-02-07 17:27:23 +04:00
## String.length(s: string) : nat
Alias for `String.size`.
## String.slice(pos1: nat, pos2: nat, s: string) : string
Get the substring of `s` between `pos1` inclusive and `pos2` inclusive. For example
the string "tata" given to the function below would return "at".
<Syntax syntax="pascaligo">
2020-02-07 17:27:23 +04:00
```pascaligo
function slice_op (const s : string) : string is string_slice(1n , 2n , s)
2020-02-07 17:27:23 +04:00
```
</Syntax>
<Syntax syntax="cameligo">
2020-02-07 17:27:23 +04:00
```cameligo
let slice_op (s: string) : string = String.slice 1n 2n s
```
</Syntax>
<Syntax syntax="reasonligo">
2020-02-07 17:27:23 +04:00
```reasonligo
let slice_op = (s: string): string => String.slice(1n, 2n, s);
```
</Syntax>
2020-02-07 17:27:23 +04:00
## String.sub(pos1: nat, pos2: nat, s: string) : string
Alias for `String.slice`.
## String.concat(s1: string, s2: string) : string
Concatenate two strings and return the result.
<Syntax syntax="pascaligo">
2020-02-07 17:27:23 +04:00
```pascaligo
function concat_op (const s : string) : string is s ^ "toto"
2020-02-07 17:27:23 +04:00
```
</Syntax>
<Syntax syntax="cameligo">
2020-02-07 17:27:23 +04:00
```cameligo
let concat_syntax (s: string) = s ^ "test_literal"
```
</Syntax>
<Syntax syntax="reasonligo">
2020-02-07 17:27:23 +04:00
```reasonligo
let concat_syntax = (s: string) => s ++ "test_literal";
```
</Syntax>