string
struct string
A UTF-8–encode string.
In Navi, strings are immutable.
Examples
let s = "Hello, world!";
assert_eq s.len(), 13;
let s = "Hello 🌍!";
assert_eq s.len(), 11;
Return the std.io.Bytes
of the string.
Return the std.io.Bytes
of the string.
Return the std.io.Bytes
of the string.
Example
let s = "Hello, world!";
let buf = s.bytes();
// buf is a Bytes object, value is [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
let s = "👋🏻 你好";
s.bytes().len(); // 15
Return true if string is empty.
Return true if string is empty.
Return true if string is empty.
Example
assert_eq "".is_empty(), true;
assert_eq "hello".is_empty(), false;
Returns an iterator over the chars of a string.
Returns an iterator over the chars of a string.
Returns an iterator over the chars of a string.
strip_suffix
pub fn strip_suffix(self, suffix: string): string
Returns a new string with the suffix removed, in case sensitive.
strip_suffix
pub fn strip_suffix(self, suffix: string): string
Returns a new string with the suffix removed, in case sensitive.
Returns a new string with the suffix removed, in case sensitive.
Example
let s = "Hello, world!".strip_suffix("!");
// "Hello, world"
strip_prefix
pub fn strip_prefix(self, prefix: string): string
Returns a new string with the prefix removed, in case sensitive.
strip_prefix
pub fn strip_prefix(self, prefix: string): string
Returns a new string with the prefix removed, in case sensitive.
Returns a new string with the prefix removed, in case sensitive.
Example
let s = "Hello, world!".strip_prefix("H");
// "ello, world!"
let s = "Hello, world!".strip_prefix("h");
// "Hello, world!"
parse_float
pub fn parse_float(self): float?
Parse a string
to a float?
.
parse_float
pub fn parse_float(self): float?
Parse a string
to a float?
.
Parse a string
to a float?
.
If the string is a valid float, return float value, else return nil.
Example
let n = "123.456".parse_float();
assert_eq n, 123.456;
let n = "123".parse_float();
assert_eq n, 123.0;
let n = "123.456.789".parse_float();
assert_eq n, nil;
let n = "abc".parse_float();
assert_eq n, nil;
let n = "123abc".parse_float();
assert_eq n, nil;
Parse a string
to a int?
.
Parse a string
to a int?
.
Parse a string
to a int?
.
If the string is a valid int, return int value, else return nil.
Example
let n = "123".parse_int();
assert_eq n, 123;
let n = "123.456".parse_int();
assert_eq n, nil;
let n = "abc".parse_int();
assert_eq n, nil;
let n = "123abc".parse_int();
assert_eq n, nil;
Return a new string with repeated copies of the string.
Return a new string with repeated copies of the string.
Return a new string with repeated copies of the string.
Example
let s = "-".repeat(5);
// "-----"
Trim whitespace from the end of a string.
Trim whitespace from the end of a string.
Trim whitespace from the end of a string.
Example
let s = " Hello, world! ";
s.trim_end(); // " Hello, world!"
trim_start
pub fn trim_start(self): string
Trim whitespace from the start of a string.
trim_start
pub fn trim_start(self): string
Trim whitespace from the start of a string.
Trim whitespace from the start of a string.
Example
let s = " Hello, world! ";
s.trim_start(); // "Hello, world! "
Trim whitespace from the start and end of a string.
Trim whitespace from the start and end of a string.
Trim whitespace from the start and end of a string.
Example
let s = " Hello, world! ";
s.trim(); // "Hello, world!"
Split a string into a list of strings.
Split a string into a list of strings.
Split a string into a list of strings.
Example
let s = "Hello, world!";
s.split(", "); // ["Hello", "world!"]
s.split(""); // ["H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"]
Get a char from a string by index(0 based).
Get a char from a string by index(0 based).
Get a char from a string by index(0 based).
Example
let s = "Hello, world!";
assert_eq s.get(0), 'H';
assert_eq s.get(7), 'w';
assert_eq s.get(12), '!';
Check if string ends with a substring, in case sensitive.
Check if string ends with a substring, in case sensitive.
Check if string ends with a substring, in case sensitive.
Example
let s = "Hello, world!";
s.ends_with("world!"); // true
s.ends_with("World!"); // false
let s = "你好,世界";
s.ends_with("界"); // true
starts_with
pub fn starts_with(self, substr: string): bool
Check if string starts with a substring, in case sensitive.
starts_with
pub fn starts_with(self, substr: string): bool
Check if string starts with a substring, in case sensitive.
Check if string starts with a substring, in case sensitive.
Example
let s = "Hello, world!";
s.starts_with("Hello"); // true
s.starts_with("hello"); // false
let s = "你好,世界!";
s.starts_with("你"); // true
Replace all matches substring with another string.
Replace all matches substring with another string.
Replace all matches substring with another string.
Example
let s = "Hello, world!";
s.replace("world", "foo"); // "Hello, foo!"
let s = "foo/bar/dar";
s.replace("/", "-"); // "foo-bar-dar"
Get a slice from a string.
Get a slice from a string.
Get a slice from a string.
The start
and end
are the start and end index of the substring (zero based in bytes).
Example
let s = "hello world.";
assert_eq s.slice(0, 5), "hello";
assert_eq s.slice(6, 11), "world";
assert_eq s.slice(0, 0), "";
let s = "hello 你好";
assert_eq s.slice(0, 5).len(), "hello".len();
assert_eq s.slice(6, 12), "你好";
to_uppercase
pub fn to_uppercase(self): string
Convert a string to uppercase.
to_uppercase
pub fn to_uppercase(self): string
Convert a string to uppercase.
Convert a string to uppercase.
Example
let s = "Hello, world!";
s.to_uppercase(); // "HELLO, WORLD!"
to_lowercase
pub fn to_lowercase(self): string
Convert a string to lowercase.
to_lowercase
pub fn to_lowercase(self): string
Convert a string to lowercase.
Convert a string to lowercase.
Example
let s = "Hello, world!";
s.to_lowercase(); // "hello, world!"
Find the first index of a substring in a string.
Find the first index of a substring in a string.
Find the first index of a substring in a string.
Return the index of the first occurrence of the substring in the string, or nil if not found.
Example
let s = "Hello, world!";
s.find("world"); // 7
s.find("foo"); // nil
Check if a string contains a substring.
Check if a string contains a substring.
Check if a string contains a substring.
Example
let s = "Hello, world!";
s.contains("world"); // true
s.contains("foo"); // false
Return the chars length of the string.
Return the chars length of the string.
Return the chars length of the string.
See also: s.len()
to get bytes length.
Example
let s = "👋🏻 你好";
assert_eq s.count(), 5;
Return the bytes length of the string.
Return the bytes length of the string.
Return the bytes length of the string.
See also: s.count()
to get chars length.
Example
let s = "Hello, world!";
s.len(); // 13
let s = "";
s.len(); // 0
let s = "👋🏻 你好";
assert_eq s.len(), 15;
operator+:2
pub fn operator+:2(self, b: string): string
operator+:2
pub fn operator+:2(self, b: string): string
operator<=:2
pub fn operator<=:2(self, b: string): bool
operator<=:2
pub fn operator<=:2(self, b: string): bool
operator<:2
pub fn operator<:2(self, b: string): bool
operator<:2
pub fn operator<:2(self, b: string): bool
operator>=:2
pub fn operator>=:2(self, b: string): bool
operator>=:2
pub fn operator>=:2(self, b: string): bool
operator>:2
pub fn operator>:2(self, b: string): bool
operator>:2
pub fn operator>:2(self, b: string): bool
operator!=:2
pub fn operator!=:2(self, b: string): bool
operator!=:2
pub fn operator!=:2(self, b: string): bool
operator==:2
pub fn operator==:2(self, b: string): bool
operator==:2
pub fn operator==:2(self, b: string): bool
equal_ignore_ascii_case
pub fn equal_ignore_ascii_case(self, b: string): bool
Checks that two strings are an ASCII case-insensitive match.
equal_ignore_ascii_case
pub fn equal_ignore_ascii_case(self, b: string): bool
Checks that two strings are an ASCII case-insensitive match.
Checks that two strings are an ASCII case-insensitive match.