diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 3d4096d95c..33620e0b39 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -459,6 +459,11 @@ pub fn (s string) f64() f64 { return strconv.atof64(s) } +// u8 returns the value of the string as u8 `'1'.u8() == u8(1)`. +pub fn (s string) byte() u8 { + return byte(strconv.common_parse_uint(s, 0, 8, false, false) or { 0 }) +} + // u16 returns the value of the string as u16 `'1'.u16() == u16(1)`. pub fn (s string) u16() u16 { return u16(strconv.common_parse_uint(s, 0, 16, false, false) or { 0 }) diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index b3105dbce0..0773006f6e 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -418,6 +418,7 @@ fn test_arr_contains() { fn test_to_num() { s := '7' assert s.int() == 7 + assert s.byte() == 7 assert s.u64() == 7 f := '71.5 hasdf' // QTODO