Skip to main content

Strings

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

module examples::strings {
use sui::object::{Self, UID};
use sui::tx_context::TxContext;

// 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
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://api.capy.art/capys/";

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