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

164 lines
3.1 KiB
Markdown
Raw Normal View History

2020-02-07 17:27:23 +04:00
---
id: string-reference
2020-03-17 19:38:41 +04:00
title: String
description: Operations for strings.
hide_table_of_contents: true
2020-02-07 17:27:23 +04:00
---
import Syntax from '@theme/Syntax';
2020-03-17 19:38:41 +04:00
import SyntaxTitle from '@theme/SyntaxTitle';
<SyntaxTitle syntax="pascaligo">
type string
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type string
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type string
</SyntaxTitle>
A sequence of characters.
<SyntaxTitle syntax="pascaligo">
function length : string -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val length : string -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let length: string => nat
</SyntaxTitle>
Get the size of a string.
[Michelson only supports ASCII strings](http://tezos.gitlab.io/whitedoc/michelson.html#constants)
2020-02-07 17:27:23 +04:00
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
2020-03-17 19:38:41 +04:00
function string_size (const s: string) : nat is String.length(s)
2020-02-07 17:27:23 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `size` and `String.size` are *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
2020-02-07 17:27:23 +04:00
```cameligo
2020-03-17 19:38:41 +04:00
let size_op (s: string) : nat = String.length s
2020-02-07 17:27:23 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `String.size` is *deprecated*.
</Syntax>
<Syntax syntax="reasonligo">
2020-02-07 17:27:23 +04:00
```reasonligo
2020-03-17 19:38:41 +04:00
let size_op = (s: string): nat => String.length(s);
2020-02-07 17:27:23 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `String.size` is *deprecated*.
2020-02-07 17:27:23 +04:00
2020-03-17 19:38:41 +04:00
</Syntax>
2020-02-07 17:27:23 +04:00
2020-03-17 19:38:41 +04:00
<SyntaxTitle syntax="pascaligo">
function sub : nat -> nat -> string -> string
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val sub : nat -> nat -> string -> string
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let sub: (nat, nat, string) => string
</SyntaxTitle>
2020-02-07 17:27:23 +04:00
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
2020-03-17 19:38:41 +04:00
function slice_op (const s : string) : string is String.sub(1n , 2n , s)
2020-02-07 17:27:23 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `string_slice` is *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
2020-02-07 17:27:23 +04:00
```cameligo
2020-03-17 19:38:41 +04:00
let slice_op (s: string) : string = String.sub 1n 2n s
2020-02-07 17:27:23 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `String.slice` is *deprecated*.
</Syntax>
<Syntax syntax="reasonligo">
2020-02-07 17:27:23 +04:00
```reasonligo
2020-03-17 19:38:41 +04:00
let slice_op = (s: string): string => String.sub(1n, 2n, s);
2020-02-07 17:27:23 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `String.slice` is *deprecated*.
2020-02-07 17:27:23 +04:00
2020-03-17 19:38:41 +04:00
</Syntax>
2020-02-07 17:27:23 +04:00
2020-03-17 19:38:41 +04:00
<SyntaxTitle syntax="pascaligo">
function concat : string -> string -> string
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val concat : string -> string -> string
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let concat: (string, string) => string
</SyntaxTitle>
2020-02-07 17:27:23 +04:00
Concatenate two strings and return the result.
<Syntax syntax="pascaligo">
2020-02-07 17:27:23 +04:00
```pascaligo
2020-03-17 19:38:41 +04:00
function concat_op (const s : string) : string is String.concat(s, "toto")
```
Alternatively:
```pascaligo
function concat_op_alt (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
2020-03-17 19:38:41 +04:00
let concat_syntax (s: string) = String.concat s "test_literal"
```
Alternatively:
```cameligo
let concat_syntax_alt (s: string) = s ^ "test_literal"
2020-02-07 17:27:23 +04:00
```
2020-03-17 19:38:41 +04:00
</Syntax>
<Syntax syntax="reasonligo">
2020-02-07 17:27:23 +04:00
```reasonligo
2020-03-17 19:38:41 +04:00
let concat_syntax = (s: string) => String.concat(s, "test_literal");
```
Alternatively:
```reasonligo
let concat_syntax_alt = (s: string) => s ++ "test_literal";
2020-02-07 17:27:23 +04:00
```
</Syntax>