Strings

Cairo doesn’t have a native type for strings but provides two ways to handle them: short strings using simple quotes and ByteArray using double quotes.

A short string is the concatenation of the ASCII encoding of its characters. Cairo uses felt252 for short strings, and as such, short strings are limited to 31 characters (⌊251 / 8⌋ = 31). Short strings can be represented with an hexadecimal value or by directly writing the string using simple quotes.

Cairo’s Core library provides a ByteArray type for handling strings and byte sequences longer than 31 characters. ByteArray strings are written using double quotes.

Example

fn main() {
    let short_string = 'aa';
    assert!(short_string == 0x6161);
    let long_string: ByteArray = "this is a string which has more than 31 characters";
    assert!(long_string.len() > 31);
}

You can experiment with the example in cairovm.codes and read more about strings in the Cairo Book.