A wrapper for String in Noir that adds some useful methods for common string operations.
In your Nargo.toml file, add the following dependency:
[dependencies]
string_utils = { tag = "main", git = "https://github.com/madztheo/noir-string-utils.git" }
If you want to use the String struct to wrap your str, you can import it like this:
use dep::string_utils::String;
And if you want to use the methods directly on the native str type, you can import the String Trait like so:
use dep::string_utils::StringTrait;
You can use the String struct to wrap your str and use the methods on it.
let my_string = String::new("Hello World!");
assert(my_string.value == "Hello World!");
assert(my_string.len == 12);
let my_str = "123";
let my_string = String::new(my_str);
let my_int = my_string.to_uint();
assert(my_int == 123);
let my_str = "0x123";
let my_string = String::new(my_str);
let my_int = my_string.from_hex_to_uint();
assert(my_int == 291);
let my_str = "2023/12/19";
let my_string = String::new(my_str);
let my_int = my_string.to_uint_slice(5, 7);
assert(my_int == 12);
let my_str = "0xa1b2c3d4e5f6";
let my_string = String::new(my_str);
let my_int = my_string.from_hex_to_uint_slice(2, 4);
assert(my_int == 161);
let my_str = "Hello World!";
let my_string = String::new(my_str);
let my_char = my_string.char_at(2);
assert(my_char == 'l');
let my_str = "Hello World!";
let my_string = String::new(my_str);
let my_index = my_string.index_of('W');
assert(my_index == 6);
let my_str = "Hello World!";
let my_string = String::new(my_str);
let my_index = my_string.index_of("World");
assert(my_index == 6);
let my_str = "Hello World!";
let my_string = String::new(my_str);
let my_index = my_string.last_index_of('l');
assert(my_index == 9);
let my_str = "Hello World!";
let my_string = String::new(my_str);
let my_index = my_string.last_index_of('rld');
assert(my_index == 8);
let my_str = "Hello World!";
let my_string = String::new(my_str);
assert(my_string.contains('W'));
assert(!my_string.contains('a'));
let my_str = "Hello World!";
let my_string = String::new(my_str);
assert(my_string.contains('Hello'));
assert(!my_string.contains('everybody'));
By importing StringTrait, you can use the methods directly on a str. No need to wrap it in a String struct.
let my_str = "123";
let my_int = my_str.to_uint();
assert(my_int == 123);
let my_str = "0x123";
let my_int = my_str.from_hex_to_uint();
assert(my_int == 291);
let my_str = "2023/12/19";
let my_int = my_str.to_uint_slice(5, 7);
assert(my_int == 12);
let my_str = "0xa1b2c3d4e5f6";
let my_int = my_str.from_hex_to_uint_slice(2, 4);
assert(my_int == 161);
let my_str = "Hello World!";
let my_char = my_str.char_at(2);
assert(my_char == 'l');
let my_str = "Hello World!";
let my_index = my_str.index_of('W');
assert(my_index == 6);
let my_str = "Hello World!";
let my_index = my_str.index_of("World");
assert(my_index == 6);
let my_str = "Hello World!";
let my_index = my_str.last_index_of('l');
assert(my_index == 9);
let my_str = "Hello World!";
let my_index = my_str.last_index_of('rld');
assert(my_index == 8);
let my_str = "Hello World!";
assert(my_str.contains('W'));
assert(!my_str.contains('a'));
let my_str = "Hello World!";
assert(my_str.contains('Hello'));
assert(!my_str.contains('everybody'));