Chapter 5 : Strings

Strings are defined using the built-in string type as follows:

let a: string = "Hello Captain Rogers"

Concatenating Strings

Strings can be concatenated using the ^ operator.

let name: string = “Captain Rogers”

let greeting: string = “Hello”

let full_greeting: string = greeting ^ ” ” ^ name

Slicing Strings

Strings can be sliced using a built-in function String.sub which takes three parameters: – an offset describing the index of first character that will be copied – the length describing the number of characters that will be copied (starting from the given offset) – the string being sliced

The function String.sub can be used as follows:

let name: string = "Captain Rogers"let slice: string = String.sub 0n 1n name

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

Length of Strings

The length of a string can be found using a built-in function String.length as follows:

let name: string = "Captain Rogers"let length: nat = String.length name // length = 14

Your mission

1 – Reassign my_ship by modifying the engine attribute (third number) from 0 to 1. Use substrings for the attributes before and after to make sure they are untouched.