Skip to main content

Strings

Move does not have a native type for strings, but it has a useful wrapper.

module examples::strings {
// Use this dependency to get a type wrapper for UTF-8 strings
use std::string::{Self, String};

/// A dummy Object that holds a String type
public struct Name has key, store {
id: UID,

/// Here it is - the String type
name: String
}

/// Create a name Object by passing raw bytes
public fun issue_name_nft(
name_bytes: vector<u8>, ctx: &mut TxContext
): Name {
Name {
id: object::new(ctx),
name: string::utf8(name_bytes)
}
}
}

String literals

Define string literals in Move as byte strings. To do so, prepend b to your string.

const IMAGE_URL: vector<u8> = b"https://example.com/image/";

To store it as a UTF-8 byte string, use the utf8 function from the std::string module.

use std::string::utf8;

let keys = vector[
utf8(b"name"),
utf8(b"link"),
utf8(b"image_url"),
utf8(b"description"),
utf8(b"project_url"),
utf8(b"creator"),
];