ligo/gitlab-pages/docs/language-basics/strings.md
2020-02-11 22:29:12 +01:00

2.1 KiB

id title
strings Strings

Strings are defined using the built-in string type like this:

const a : string = "Hello Alice"
let a : string = "Hello Alice"
let a : string = "Hello Alice";

Concatenating Strings

Strings can be concatenated using the ^ operator.

const name : string = "Alice"
const greeting : string = "Hello"
const full_greeting : string = greeting ^ " " ^ name

Strings can be concatenated using the ^ operator.

let name : string = "Alice"
let greeting : string = "Hello"
let full_greeting : string = greeting ^ " " ^ name

Strings can be concatenated using the ++ operator.

let name : string = "Alice";
let greeting : string = "Hello";
let full_greeting : string = greeting ++ " " ++ name;

Slicing Strings

Strings can be sliced using a built-in function:

const name  : string = "Alice"
const slice : string = string_slice (0n, 1n, name)
let name  : string = "Alice"
let slice : string = String.slice 0n 1n name
let name  : string = "Alice";
let slice : string = String.slice (0n, 1n, name);

⚠️ Notice that the offset and length of the slice are natural numbers.

Length of Strings

The length of a string can be found using a built-in function:

const name : string = "Alice"
const length : nat = size (name)   // length = 5
let name : string = "Alice"
let length : nat = String.size name  // length = 5
let name : string = "Alice";
let length : nat = String.size (name);  // length == 5