xur_

> built-in type

str

methods of str values

in the browser

The methods below are on string values. They are available on any str value directly, without importing a module. The std::str module adds the chr function and the str::Error type.

Strings are UTF-8. Lengths, indices and offsets count characters (Unicode scalar values), not bytes: "αβ".len() is 2. Strings never change; methods that edit return a new string.

slice, insert and remove take indices that may be negative, counting from the end: -1 is the position before the last character. Indices past either end are clamped to the string, so these methods never fail.

> methods

# str.bytes() -> list<i64>

The UTF-8 bytes, each as an integer 0–255.

# str.chars() -> list<str>

The characters, each as a one-character string.

# str.contains(part: str) -> bool

Whether part occurs in the string.

# str.ends_with(suffix: str) -> bool

Whether the string ends with suffix.

# str.find(needle: str) -> Option<i64>

The character index of the first occurrence of needle, or None.

# str.from_hex() -> Result<bytes, Error>

Parses the string as pairs of hex digits, ignoring whitespace, the same as bytes::from_hex. The error is a bytes::Error: BadHex for an odd number of digits or a non-hex character.

# str.insert(index: i64, substr: str) -> str

The string with substr inserted before the character at index: "helo".insert(3, "l") is "hello".

# str.is_empty() -> bool

Whether the string has no characters.

# str.len() -> i64

The number of characters.

# str.lines() -> list<str>

The lines of the string, split at \\n or \\r\\n. A final line ending does not add an empty line.

# str.ord() -> Result<u32, Error>

The Unicode codepoint of a one-character string: "A".ord() is Ok(65). Any other length gives Err(str::Error::NotSingleChar).

# str.pad_left(width: usize, fill: str) -> str

The string padded on the left or right to width characters, repeating the characters of fill as needed. A string already width long or longer, or an empty fill, leaves the string unchanged.

# str.pad_right(width: usize, fill: str) -> str

The string padded on the left or right to width characters, repeating the characters of fill as needed. A string already width long or longer, or an empty fill, leaves the string unchanged.

# str.parse_float() -> Result<f64, Error>

The string, without surrounding whitespace, as a float. Anything else gives Err(str::Error::Parse).

# str.parse_hex() -> Result<i64, Error>

The string, without surrounding whitespace and an optional 0x or 0X prefix, as a hexadecimal integer: "0xff".parse_hex() is Ok(255). Anything else gives Err(str::Error::Parse).

# str.parse_int() -> Result<i64, Error>

The string, without surrounding whitespace, as a decimal integer. Anything else gives Err(str::Error::Parse).

# str.remove(start: i64, end: i64) -> str

The string without the characters from start up to, not including, end: "hello".remove(1, 4) is "ho". When start is not before end, the string is unchanged.

# str.repeat(n: u32) -> str

The string repeated n times.

# str.replace(from: str, to: str) -> str

The string with every occurrence of from replaced by to.

# str.rfind(needle: str) -> Option<i64>

The character index of the last occurrence of needle, or None.

# str.slice(start: i64, end: i64) -> str

The characters from start up to, not including, end. "hello".slice(1, 4) is "ell" and "hello".slice(0, -1) is "hell". When start is not before end, the result is empty.

# str.split(separator: str) -> list<str>

The pieces between occurrences of separator. Empty pieces are kept: "a,,b".split(",") is ["a", "", "b"].

# str.starts_with(prefix: str) -> bool

Whether the string begins with prefix.

# str.to_bytes() -> bytes

The UTF-8 bytes as a bytes buffer.

# str.to_lower() -> str

The string in lowercase, using Unicode case rules.

# str.to_upper() -> str

The string in uppercase, using Unicode case rules.

# str.trim() -> str

The string without leading and trailing whitespace.

# str.trim_end() -> str

The string without trailing whitespace.

# str.trim_start() -> str

The string without leading whitespace.

> examples

use std::io;

fn main() {
    let s = "  Hello, World!  ";
    io::println("{}", s.trim());
    io::println("{}", s.trim().to_upper());
    io::println("{}", "a,b,c".split(",").join(" | "));
    io::println("{}", "42".parse_int().unwrap());
}