String functions
Alumina "strings" are simply byte slices (&[u8]
) without any assumed encoding.
An exception to this are string literals in code. They are treated as UTF-8
(but not required to be valid UTF-8), e.g.
assert_eq!("😊", "\xf0\x9f\x98\x8a");
assert_eq!("\u{1f60a}", "\xf0\x9f\x98\x8a");
"\xe2\x82\x28\x00\xf0\x01\x02"; // Not valid UTF-8, but a valid string literal
Run this example
This module contains various functions for working with strings, a heap-allocated string type (StringBuf). Unicode-specific functionality is provided by the unicode module.
Modules
Structs
needle
with replacement
.
Functions
-
Returns
true
if character is an ASCII whitespace character,false
otherwise. -
Returns
true
if character is a decimal digit,false
otherwise. -
Convert character to lower case.
If character is not an ASCII uppercase letter, returns the same character.
-
Convert character to upper case.
If character is not an ASCII lowercase letter, returns the same character.
-
Returns
true
if string starts with the given prefix,false
otherwise.Example
use std::string::starts_with; assert!("hello world".starts_with("hello")); assert!(!"hello world".starts_with("world"));
Run this example -
Returns
true
if string ends with the given suffix,false
otherwise.Example
use std::string::ends_with; assert!("hello world".ends_with("world")); assert!(!"hello world".ends_with("hello"));
Run this example -
Return string without the given prefix.
If the string does not start with the given prefix, returns the string unchanged.
Example
use std::string::trim_prefix; assert_eq!("hello world".trim_prefix("hello"), " world"); assert_eq!("hello world".trim_prefix("world"), "hello world");
Run this example -
Return string without the given suffix.
If the string does not end with the given suffix, returns the string unchanged.
Example
use std::string::trim_suffix; assert_eq!("hello world".trim_suffix("world"), "hello "); assert_eq!("hello world".trim_suffix("hello"), "hello world");
Run this example -
Finds a substring in a string.
It returns the index of the first occurrence of
needle
inhaystack
, orOption::none()
ifneedle
is not part ofhaystack
.Example
use std::string::find; assert_eq!("hello world".find("world"), Option::some(6usize)); assert_eq!("hello world".find("worldx"), Option::none());
Run this example -
Finds the index of the first occurrence of a character in a string.
Example
use std::string::find_char; assert_eq!("hello world".find_char('o'), Option::some(4usize)); assert_eq!("hello world".find_char('x'), Option::none());
Run this example -
Returns
true
ifneedle
is a substring of self,false
otherwise.Example
use std::string::contains; assert!("hello world".contains("world")); assert!(!"hello world".contains("chtulhu"));
Run this example -
Trims ASCII whitespace from the start of the string.
Example
use std::string::trim_start; assert_eq!(" hello world ".trim_start(), "hello world ");
Run this example -
Trims ASCII whitespace from the end of the string.
Example
use std::string::trim_end; assert_eq!(" hello world ".trim_end(), " hello world");
Run this example -
Trims the whitespace from the start and end of the string.
Example
use std::string::trim; assert_eq!(" hello world ".trim(), "hello world");
Run this example -
fn split(self: &[u8], sep: &[u8]) -> SplitIterator
Returns an iterator over the parts of the string split by the given delimiter.
Example
use std::string::split; let iter = "192.168.0.1".split("."); assert_eq!(iter.next(), Option::some("192")); assert_eq!(iter.next(), Option::some("168")); assert_eq!(iter.next(), Option::some("0")); assert_eq!(iter.next(), Option::some("1")); assert_eq!(iter.next(), Option::none());
Run this example -
Replaces all occurrences of
needle
withreplacement
, returning a new string.See also replace_fmt for a non-allocating variant.
-
fn replace_fmt(self: &[u8], needle: &[u8], replacement: &[u8]) -> ReplaceAdapter
Returns an object that formats to the string with all
needle
occurrences replaced withreplacement
.Example
use std::string::replace_fmt; // Prints "I like cake" println!("{}", "I like pie".replace_fmt("pie", "cake"));
Run this example -
Join the parts of a string with the given delimiter, returning a new string.
See also join_fmt for a non-allocating string formatting adapter.
-
fn join_fmt<It>(self: &[u8], iter: &mut It) -> JoinAdapter<It>
Returns an object that formats to parts joined with the separartor.
Example
use std::string::join_fmt; // Prints 192.168.0.1 println!("{}", ".".join_fmt(&["192", "168", "0", "1"].iter()));
Run this example -
Parses an integer with the given radix.
See also IntegerParsable, a mixin available on all integer types
Consts
-
Digit characters for up to base 36.