String manipulation
Strings are a packed UTF-8 representation of text, stored as buffers of u8 bytes. The buffer is not null terminated. Strings should be indexed in bytes, for efficiency, but UTF-8 unsafe operations should be avoided.
AnyLineIterator
- An iterator over the lines of a string, separated by either \\n
or (\\r\\n
).ByteIterator
- External iterator for a string's bytesByteRevIterator
- External iterator for a string's bytes in reverse orderCharOffsetRevIterator
- External iterator for a string's characters and their byte offsets in reverse orderCharRSplitIterator
- An iterator over the substrings of a string, separated by sep
, starting from the back of the string.CharRevIterator
- External iterator for a string's characters in reverse orderWordIterator
- An iterator over the words of a string, separated by an sequence of whitespaceCharIterator
- External iterator for a string's charactersCharOffsetIterator
- External iterator for a string's characters and their byte offsetsCharRange
CharSplitIterator
- An iterator over the substrings of a string, separated by sep
.CharSplitNIterator
- An iterator over the substrings of a string, separated by sep
, splitting at most count
times.MatchesIndexIterator
- An iterator over the start and end indices of the matches of a substring within a larger stringStrSplitIterator
- An iterator over the substrings of a string separated by a given search stringCharEq
- Something that can be used to compare against a characterOwnedStr
Str
- Any string that can be represented as a sliceStrSlice
StrVector
of ToStr for ~str
of ToStr for &'self str where <'self>
of ToStr for @str
of StrVector for &'self [S] where <'self, S: Str>
of CharEq for char
of CharEq for &'self fn(char) -> bool where <'self>
of CharEq for extern "Rust" fn(char) -> bool
of CharEq for &'self [C] where <'self, C: CharEq>
of ::std::clone::Clone for CharIterator<'self> where <'self>
- Automatically derived.of Iterator<char> for CharIterator<'self> where <'self>
of DoubleEndedIterator<char> for CharIterator<'self> where <'self>
of ::std::clone::Clone for CharOffsetIterator<'self> where <'self>
- Automatically derived.of Iterator<(uint, char)> for CharOffsetIterator<'self> where <'self>
of DoubleEndedIterator<(uint, char)> for CharOffsetIterator<'self> where <'self>
of ::std::clone::Clone for CharSplitIterator<'self, Sep> where <'self, Sep: ::std::clone::Clone>
- Automatically derived.of ::std::clone::Clone for CharSplitNIterator<'self, Sep> where <'self, Sep: ::std::clone::Clone>
- Automatically derived.of Iterator<&'self str> for CharSplitIterator<'self, Sep> where <'self, Sep: CharEq>
of DoubleEndedIterator<&'self str> for CharSplitIterator<'self, Sep> where <'self, Sep: CharEq>
of Iterator<&'self str> for CharSplitNIterator<'self, Sep> where <'self, Sep: CharEq>
of ::std::clone::Clone for MatchesIndexIterator<'self> where <'self>
- Automatically derived.of ::std::clone::Clone for StrSplitIterator<'self> where <'self>
- Automatically derived.of Iterator<(uint, uint)> for MatchesIndexIterator<'self> where <'self>
of Iterator<&'self str> for StrSplitIterator<'self> where <'self>
of ::std::clone::Clone for NormalizationForm
- Automatically derived.of ::std::clone::Clone for NormalizationIterator<'self> where <'self>
- Automatically derived.of Iterator<char> for NormalizationIterator<'self> where <'self>
of Str for &'self str where <'self>
of Str for ~str where <'self>
of Str for @str where <'self>
of Container for &'self str where <'self>
of Container for ~str
of Container for @str
of Mutable for ~str
of StrSlice<'self> for &'self str where <'self>
- Extension methods for stringsof OwnedStr for ~str
of Clone for ~str
of DeepClone for ~str
of Clone for @str
of DeepClone for @str
of FromIterator<char> for ~str
of Extendable<char> for ~str
of Default for &'self str where <'self>
of Default for ~str
of Default for @str
eq
- Bytewise string equalityeq_slice
- Bytewise slice equalityfrom_byte
- Convert a byte to a UTF-8 stringfrom_char
- Convert a char to a stringfrom_chars
- Convert a vector of chars to a stringfrom_utf16
- Allocates a new string from the utf-16 slice providedfrom_utf8
- Convert a vector of bytes to a new UTF-8 stringfrom_utf8_opt
- Convert a vector of bytes to a new UTF-8 string, if possiblefrom_utf8_owned
- Consumes a vector of bytes to create a new utf-8 stringfrom_utf8_owned_opt
- Consumes a vector of bytes to create a new utf-8 stringfrom_utf8_slice
- Converts a vector to a string slice without performing any allocations.from_utf8_slice_opt
- Converts a vector to a string slice without performing any allocations.is_utf16
- Determines if a vector of u16
contains valid UTF-16is_utf8
- Determines if a vector of bytes contains valid UTF-8replace
- Replace all occurrences of one string with anotherutf16_chars
utf8_char_width
- Given a first byte, determine how many bytes are in this UTF-8 characterwith_capacity
- Allocates a new string with the specified capacitystr::not_utf8
str::raw
- Unsafe operationsstr::traits
AnyLineIterator
type AnyLineIterator<'self> = Map<'self, &'self str, &'self str, CharSplitIterator<'self, char>>
An iterator over the lines of a string, separated by either \\n
or (\\r\\n
).
ByteIterator
type ByteIterator<'self> = Map<'self, &'self u8, u8, vec::VecIterator<'self, u8>>
External iterator for a string's bytes. Use with the std::iterator
module.
ByteRevIterator
type ByteRevIterator<'self> = Invert<ByteIterator<'self>>
External iterator for a string's bytes in reverse order. Use with the std::iterator
module.
CharOffsetRevIterator
type CharOffsetRevIterator<'self> = Invert<CharOffsetIterator<'self>>
External iterator for a string's characters and their byte offsets in reverse order. Use with the std::iterator
module.
CharRSplitIterator
type CharRSplitIterator<'self, Sep> = Invert<CharSplitIterator<'self, Sep>>
An iterator over the substrings of a string, separated by sep
, starting from the back of the string.
CharRevIterator
type CharRevIterator<'self> = Invert<CharIterator<'self>>
External iterator for a string's characters in reverse order. Use with the std::iterator
module.
WordIterator
type WordIterator<'self> = Filter<'self,
&'self str, CharSplitIterator<'self, extern "Rust" fn(char) -> bool>>
An iterator over the words of a string, separated by an sequence of whitespace
CharIterator
pub struct CharIterator<'self> {
/// The slice remaining to be iterated
priv string: &'self str,
}
External iterator for a string's characters. Use with the std::iterator
module.
CharOffsetIterator
pub struct CharOffsetIterator<'self> {
/// The original string to be iterated
priv string: &'self str,
priv iter: CharIterator<'self>,
}
External iterator for a string's characters and their byte offsets. Use with the std::iterator
module.
CharRange
pub struct CharRange {
ch: char,
next: uint,
}
CharSplitIterator
pub struct CharSplitIterator<'self, Sep> {
/// The slice remaining to be iterated
priv string: &'self str,
priv sep: Sep,
/// Whether an empty string at the end is allowed
priv allow_trailing_empty: bool,
priv only_ascii: bool,
priv finished: bool,
}
An iterator over the substrings of a string, separated by sep
.
CharSplitNIterator
pub struct CharSplitNIterator<'self, Sep> {
priv iter: CharSplitIterator<'self, Sep>,
/// The number of splits remaining
priv count: uint,
priv invert: bool,
}
An iterator over the substrings of a string, separated by sep
, splitting at most count
times.
MatchesIndexIterator
pub struct MatchesIndexIterator<'self> {
priv haystack: &'self str,
priv needle: &'self str,
priv position: uint,
}
An iterator over the start and end indices of the matches of a substring within a larger string
StrSplitIterator
pub struct StrSplitIterator<'self> {
priv it: MatchesIndexIterator<'self>,
priv last_end: uint,
priv finished: bool,
}
An iterator over the substrings of a string separated by a given search string
CharEq
Something that can be used to compare against a character
matches
fn matches(&self, char) -> bool
Determine if the splitter should split at the given character
only_ascii
fn only_ascii(&self) -> bool
Indicate if this is only concerned about ASCII characters, which can allow for a faster implementation.
OwnedStr
push_str_no_overallocate
fn push_str_no_overallocate(&mut self, rhs: &str)
push_str
fn push_str(&mut self, rhs: &str)
push_char
fn push_char(&mut self, c: char)
pop_char
fn pop_char(&mut self) -> char
shift_char
fn shift_char(&mut self) -> char
unshift_char
fn unshift_char(&mut self, ch: char)
append
fn append(self, rhs: &str) -> ~str
reserve
fn reserve(&mut self, n: uint)
reserve_at_least
fn reserve_at_least(&mut self, n: uint)
capacity
fn capacity(&self) -> uint
truncate
fn truncate(&mut self, len: uint)
into_bytes
fn into_bytes(self) -> ~[u8]
as_mut_buf
fn as_mut_buf<T>(&mut self, f: &fn(*mut u8, uint) -> T) -> T
Work with the mutable byte buffer and length of a slice.
The buffer does not have a null terminator.
The caller must make sure any mutations to this buffer keep the string valid UTF-8!
Str
Any string that can be represented as a slice
as_slice
fn as_slice<'a>(&'a self) -> &'a str
Work with self
as a slice.
into_owned
fn into_owned(self) -> ~str
Convert self
into a ~str, not making a copy if possible
StrSlice
contains
fn contains<'a>(&self, needle: &'a str) -> bool
contains_char
fn contains_char(&self, needle: char) -> bool
iter
fn iter(&self) -> CharIterator<'self>
rev_iter
fn rev_iter(&self) -> CharRevIterator<'self>
byte_iter
fn byte_iter(&self) -> ByteIterator<'self>
byte_rev_iter
fn byte_rev_iter(&self) -> ByteRevIterator<'self>
char_offset_iter
fn char_offset_iter(&self) -> CharOffsetIterator<'self>
char_offset_rev_iter
fn char_offset_rev_iter(&self) -> CharOffsetRevIterator<'self>
split_iter
fn split_iter<Sep: CharEq>(&self, sep: Sep) -> CharSplitIterator<'self, Sep>
splitn_iter
fn splitn_iter<Sep: CharEq>(&self, sep: Sep, count: uint) ->
CharSplitNIterator<'self, Sep>
split_terminator_iter
fn split_terminator_iter<Sep: CharEq>(&self, sep: Sep) ->
CharSplitIterator<'self, Sep>
rsplit_iter
fn rsplit_iter<Sep: CharEq>(&self, sep: Sep) -> CharRSplitIterator<'self, Sep>
rsplitn_iter
fn rsplitn_iter<Sep: CharEq>(&self, sep: Sep, count: uint) ->
CharSplitNIterator<'self, Sep>
matches_index_iter
fn matches_index_iter(&self, sep: &'self str) -> MatchesIndexIterator<'self>
split_str_iter
fn split_str_iter(&self, &'self str) -> StrSplitIterator<'self>
line_iter
fn line_iter(&self) -> CharSplitIterator<'self, char>
any_line_iter
fn any_line_iter(&self) -> AnyLineIterator<'self>
word_iter
fn word_iter(&self) -> WordIterator<'self>
nfd_iter
fn nfd_iter(&self) -> NormalizationIterator<'self>
nfkd_iter
fn nfkd_iter(&self) -> NormalizationIterator<'self>
ends_with
fn ends_with(&self, needle: &str) -> bool
is_whitespace
fn is_whitespace(&self) -> bool
is_alphanumeric
fn is_alphanumeric(&self) -> bool
char_len
fn char_len(&self) -> uint
slice
fn slice(&self, begin: uint, end: uint) -> &'self str
slice_from
fn slice_from(&self, begin: uint) -> &'self str
slice_to
fn slice_to(&self, end: uint) -> &'self str
slice_chars
fn slice_chars(&self, begin: uint, end: uint) -> &'self str
starts_with
fn starts_with(&self, needle: &str) -> bool
escape_default
fn escape_default(&self) -> ~str
escape_unicode
fn escape_unicode(&self) -> ~str
trim
fn trim(&self) -> &'self str
trim_left
fn trim_left(&self) -> &'self str
trim_right
fn trim_right(&self) -> &'self str
trim_chars
fn trim_chars<C: CharEq>(&self, to_trim: &C) -> &'self str
trim_left_chars
fn trim_left_chars<C: CharEq>(&self, to_trim: &C) -> &'self str
trim_right_chars
fn trim_right_chars<C: CharEq>(&self, to_trim: &C) -> &'self str
replace
fn replace(&self, from: &str, to: &str) -> ~str
to_owned
fn to_owned(&self) -> ~str
to_managed
fn to_managed(&self) -> @str
to_utf16
fn to_utf16(&self) -> ~[u16]
is_char_boundary
fn is_char_boundary(&self, index: uint) -> bool
char_range_at
fn char_range_at(&self, start: uint) -> CharRange
char_at
fn char_at(&self, i: uint) -> char
char_range_at_reverse
fn char_range_at_reverse(&self, start: uint) -> CharRange
char_at_reverse
fn char_at_reverse(&self, i: uint) -> char
as_bytes
fn as_bytes(&self) -> &'self [u8]
find
fn find<C: CharEq>(&self, search: C) -> Option<uint>
rfind
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>
find_str
fn find_str(&self, &str) -> Option<uint>
repeat
fn repeat(&self, nn: uint) -> ~str
slice_shift_char
fn slice_shift_char(&self) -> (char, &'self str)
lev_distance
fn lev_distance(&self, t: &str) -> uint
subslice_offset
fn subslice_offset(&self, inner: &str) -> uint
as_imm_buf
fn as_imm_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T
StrVector
concat
fn concat(&self) -> ~str
connect
fn connect(&self, sep: &str) -> ~str
ToStr
for ~str
to_str
fn to_str(&self) -> ~str
ToStr
for &'self str
where <'self>
to_str
fn to_str(&self) -> ~str
ToStr
for @str
to_str
fn to_str(&self) -> ~str
StrVector
for &'self [S]
where <'self, S: Str>
concat
fn concat(&self) -> ~str
Concatenate a vector of strings.
connect
fn connect(&self, sep: &str) -> ~str
Concatenate a vector of strings, placing a given separator between each.
CharEq
for char
matches
fn matches(&self, c: char) -> bool
only_ascii
fn only_ascii(&self) -> bool
CharEq
for &'self fn(char) -> bool
where <'self>
matches
fn matches(&self, c: char) -> bool
only_ascii
fn only_ascii(&self) -> bool
CharEq
for extern "Rust" fn(char) -> bool
matches
fn matches(&self, c: char) -> bool
only_ascii
fn only_ascii(&self) -> bool
CharEq
for &'self [C]
where <'self, C: CharEq>
matches
fn matches(&self, c: char) -> bool
only_ascii
fn only_ascii(&self) -> bool
::std::clone::Clone
for CharIterator<'self>
where <'self>
Automatically derived.
clone
fn clone(&self) -> CharIterator<'self>
Iterator<char>
for CharIterator<'self>
where <'self>
next
fn next(&mut self) -> Option<char>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
DoubleEndedIterator<char>
for CharIterator<'self>
where <'self>
next_back
fn next_back(&mut self) -> Option<char>
::std::clone::Clone
for CharOffsetIterator<'self>
where <'self>
Automatically derived.
clone
fn clone(&self) -> CharOffsetIterator<'self>
Iterator<(uint, char)>
for CharOffsetIterator<'self>
where <'self>
next
fn next(&mut self) -> Option<(uint, char)>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
DoubleEndedIterator<(uint, char)>
for CharOffsetIterator<'self>
where <'self>
next_back
fn next_back(&mut self) -> Option<(uint, char)>
::std::clone::Clone
for CharSplitIterator<'self, Sep>
where <'self, Sep: ::std::clone::Clone>
Automatically derived.
clone
fn clone(&self) -> CharSplitIterator<'self, Sep>
::std::clone::Clone
for CharSplitNIterator<'self, Sep>
where <'self, Sep: ::std::clone::Clone>
Automatically derived.
clone
fn clone(&self) -> CharSplitNIterator<'self, Sep>
Iterator<&'self str>
for CharSplitIterator<'self, Sep>
where <'self, Sep: CharEq>
next
fn next(&mut self) -> Option<&'self str>
DoubleEndedIterator<&'self str>
for CharSplitIterator<'self, Sep>
where <'self, Sep: CharEq>
next_back
fn next_back(&mut self) -> Option<&'self str>
Iterator<&'self str>
for CharSplitNIterator<'self, Sep>
where <'self, Sep: CharEq>
next
fn next(&mut self) -> Option<&'self str>
::std::clone::Clone
for MatchesIndexIterator<'self>
where <'self>
Automatically derived.
clone
fn clone(&self) -> MatchesIndexIterator<'self>
::std::clone::Clone
for StrSplitIterator<'self>
where <'self>
Automatically derived.
clone
fn clone(&self) -> StrSplitIterator<'self>
Iterator<(uint, uint)>
for MatchesIndexIterator<'self>
where <'self>
next
fn next(&mut self) -> Option<(uint, uint)>
Iterator<&'self str>
for StrSplitIterator<'self>
where <'self>
next
fn next(&mut self) -> Option<&'self str>
::std::clone::Clone
for NormalizationForm
Automatically derived.
clone
fn clone(&self) -> NormalizationForm
::std::clone::Clone
for NormalizationIterator<'self>
where <'self>
Automatically derived.
clone
fn clone(&self) -> NormalizationIterator<'self>
Iterator<char>
for NormalizationIterator<'self>
where <'self>
next
fn next(&mut self) -> Option<char>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
Str
for &'self str
where <'self>
as_slice
fn as_slice<'a>(&'a self) -> &'a str
into_owned
fn into_owned(self) -> ~str
Str
for ~str
where <'self>
as_slice
fn as_slice<'a>(&'a self) -> &'a str
into_owned
fn into_owned(self) -> ~str
Str
for @str
where <'self>
as_slice
fn as_slice<'a>(&'a self) -> &'a str
into_owned
fn into_owned(self) -> ~str
Container
for &'self str
where <'self>
len
fn len(&self) -> uint
Container
for ~str
len
fn len(&self) -> uint
Container
for @str
len
fn len(&self) -> uint
Mutable
for ~str
clear
fn clear(&mut self)
Remove all content, make the string empty
StrSlice<'self>
for &'self str
where <'self>
Extension methods for strings
contains
fn contains<'a>(&self, needle: &'a str) -> bool
Returns true if one string contains another
contains_char
fn contains_char(&self, needle: char) -> bool
Returns true if a string contains a char.
iter
fn iter(&self) -> CharIterator<'self>
An iterator over the characters of self
. Note, this iterates over unicode code-points, not unicode graphemes.
let v: ~[char] = "abc åäö".iter().collect();
assert_eq!(v, ~['a', 'b', 'c', ' ', 'å', 'ä', 'ö']);
rev_iter
fn rev_iter(&self) -> CharRevIterator<'self>
An iterator over the characters of self
, in reverse order.
byte_iter
fn byte_iter(&self) -> ByteIterator<'self>
An iterator over the bytes of self
byte_rev_iter
fn byte_rev_iter(&self) -> ByteRevIterator<'self>
An iterator over the bytes of self
, in reverse order
char_offset_iter
fn char_offset_iter(&self) -> CharOffsetIterator<'self>
An iterator over the characters of self
and their byte offsets.
char_offset_rev_iter
fn char_offset_rev_iter(&self) -> CharOffsetRevIterator<'self>
An iterator over the characters of self
and their byte offsets, in reverse order.
split_iter
fn split_iter<Sep: CharEq>(&self, sep: Sep) -> CharSplitIterator<'self, Sep>
An iterator over substrings of self
, separated by characters matched by sep
.
let v: ~[&str] = "Mary had a little lamb".split_iter(' ').collect();
assert_eq!(v, ~["Mary", "had", "a", "little", "lamb"]);
let v: ~[&str] = "abc1def2ghi".split_iter(|c: char| c.is_digit()).collect();
assert_eq!(v, ~["abc", "def", "ghi"]);
splitn_iter
fn splitn_iter<Sep: CharEq>(&self, sep: Sep, count: uint) ->
CharSplitNIterator<'self, Sep>
An iterator over substrings of self
, separated by characters matched by sep
, restricted to splitting at most count
times.
split_terminator_iter
fn split_terminator_iter<Sep: CharEq>(&self, sep: Sep) ->
CharSplitIterator<'self, Sep>
An iterator over substrings of self
, separated by characters matched by sep
.
Equivalent to split_iter
, except that the trailing substring is skipped if empty (terminator semantics).
let v: ~[&str] = "A.B.".split_terminator_iter('.').collect();
assert_eq!(v, ~["A", "B"]);
rsplit_iter
fn rsplit_iter<Sep: CharEq>(&self, sep: Sep) -> CharRSplitIterator<'self, Sep>
An iterator over substrings of self
, separated by characters matched by sep
, in reverse order
let v: ~[&str] = "Mary had a little lamb".rsplit_iter(' ').collect();
assert_eq!(v, ~["lamb", "little", "a", "had", "Mary"]);
rsplitn_iter
fn rsplitn_iter<Sep: CharEq>(&self, sep: Sep, count: uint) ->
CharSplitNIterator<'self, Sep>
An iterator over substrings of self
, separated by characters matched by sep
, starting from the end of the string. Restricted to splitting at most count
times.
matches_index_iter
fn matches_index_iter(&self, sep: &'self str) -> MatchesIndexIterator<'self>
An iterator over the start and end indices of each match of sep
within self
.
split_str_iter
fn split_str_iter(&self, sep: &'self str) -> StrSplitIterator<'self>
An iterator over the substrings of self
separated by sep
.
let v: ~[&str] = "abcXXXabcYYYabc".split_str_iter("abc").collect()
assert_eq!(v, ["", "XXX", "YYY", ""]);
line_iter
fn line_iter(&self) -> CharSplitIterator<'self, char>
An iterator over the lines of a string (subsequences separated by \\n
).
any_line_iter
fn any_line_iter(&self) -> AnyLineIterator<'self>
An iterator over the lines of a string, separated by either \\n
or (\\r\\n
).
word_iter
fn word_iter(&self) -> WordIterator<'self>
An iterator over the words of a string (subsequences separated by any sequence of whitespace).
nfd_iter
fn nfd_iter(&self) -> NormalizationIterator<'self>
Returns the string in Unicode Normalization Form D (canonical decomposition)
nfkd_iter
fn nfkd_iter(&self) -> NormalizationIterator<'self>
Returns the string in Unicode Normalization Form KD (compatibility decomposition)
is_whitespace
fn is_whitespace(&self) -> bool
Returns true if the string contains only whitespace
Whitespace characters are determined by char::is_whitespace
is_alphanumeric
fn is_alphanumeric(&self) -> bool
Returns true if the string contains only alphanumerics
Alphanumeric characters are determined by char::is_alphanumeric
char_len
fn char_len(&self) -> uint
Returns the number of characters that a string holds
slice
fn slice(&self, begin: uint, end: uint) -> &'self str
Returns a slice of the given string from the byte range [begin
..end
)
Fails when begin
and end
do not point to valid characters or beyond the last character of the string
slice_from
fn slice_from(&self, begin: uint) -> &'self str
Returns a slice of the string from begin
to its end.
Fails when begin
does not point to a valid character, or is out of bounds.
slice_to
fn slice_to(&self, end: uint) -> &'self str
Returns a slice of the string from the beginning to byte end
.
Fails when end
does not point to a valid character, or is out of bounds.
slice_chars
fn slice_chars(&self, begin: uint, end: uint) -> &'self str
Returns a slice of the string from the char range [begin
..end
).
Fails if begin
> end
or the either begin
or end
are beyond the last character of the string.
starts_with
fn starts_with<'a>(&self, needle: &'a str) -> bool
Returns true if needle
is a prefix of the string.
ends_with
fn ends_with(&self, needle: &str) -> bool
Returns true if needle
is a suffix of the string.
escape_default
fn escape_default(&self) -> ~str
Escape each char in s
with char::escape_default.
escape_unicode
fn escape_unicode(&self) -> ~str
Escape each char in s
with char::escape_unicode.
trim
fn trim(&self) -> &'self str
Returns a string with leading and trailing whitespace removed
trim_left
fn trim_left(&self) -> &'self str
Returns a string with leading whitespace removed
trim_right
fn trim_right(&self) -> &'self str
Returns a string with trailing whitespace removed
trim_chars
fn trim_chars<C: CharEq>(&self, to_trim: &C) -> &'self str
Returns a string with characters that match to_trim
removed.
assert_eq!("11foo1bar11".trim_chars(&'1'), "foo1bar")
assert_eq!("12foo1bar12".trim_chars(& &['1', '2']), "foo1bar")
assert_eq!("123foo1bar123".trim_chars(&|c: char| c.is_digit()), "foo1bar")
trim_left_chars
fn trim_left_chars<C: CharEq>(&self, to_trim: &C) -> &'self str
Returns a string with leading chars_to_trim
removed.
assert_eq!("11foo1bar11".trim_left_chars(&'1'), "foo1bar11")
assert_eq!("12foo1bar12".trim_left_chars(& &['1', '2']), "foo1bar12")
assert_eq!("123foo1bar123".trim_left_chars(&|c: char| c.is_digit()), "foo1bar123")
trim_right_chars
fn trim_right_chars<C: CharEq>(&self, to_trim: &C) -> &'self str
Returns a string with trailing chars_to_trim
removed.
assert_eq!("11foo1bar11".trim_right_chars(&'1'), "11foo1bar")
assert_eq!("12foo1bar12".trim_right_chars(& &['1', '2']), "12foo1bar")
assert_eq!("123foo1bar123".trim_right_chars(&|c: char| c.is_digit()), "123foo1bar")
replace
fn replace(&self, from: &str, to: &str) -> ~str
Replace all occurrences of one string with another
The original string with all occurances of from
replaced with to
to_owned
fn to_owned(&self) -> ~str
Copy a slice into a new unique str
to_managed
fn to_managed(&self) -> @str
to_utf16
fn to_utf16(&self) -> ~[u16]
Converts to a vector of u16
encoded as UTF-16.
is_char_boundary
fn is_char_boundary(&self, index: uint) -> bool
Returns false if the index points into the middle of a multi-byte character sequence.
char_range_at
fn char_range_at(&self, i: uint) -> CharRange
Pluck a character out of a string and return the index of the next character.
This function can be used to iterate over the unicode characters of a string.
let s = "中华Việt Nam";
let i = 0u;
while i < s.len() {
let CharRange {ch, next} = s.char_range_at(i);
printfln!("%u: %c", i, ch);
i = next;
}
0: 中
3: 华
6: V
7: i
8: ệ
11: t
12:
13: N
14: a
15: m
A record {ch: char, next: uint} containing the char value and the byte index of the next unicode character.
If i
is greater than or equal to the length of the string. If i
is not the index of the beginning of a valid UTF-8 character.
char_at
fn char_at(&self, i: uint) -> char
Plucks the character starting at the i
th byte of a string
char_range_at_reverse
fn char_range_at_reverse(&self, start: uint) -> CharRange
Given a byte position and a str, return the previous char and its position.
This function can be used to iterate over a unicode string in reverse.
Returns 0 for next index if called on start index 0.
char_at_reverse
fn char_at_reverse(&self, i: uint) -> char
Plucks the character ending at the i
th byte of a string
as_bytes
fn as_bytes(&self) -> &'self [u8]
Work with the byte buffer of a string as a byte slice.
find
fn find<C: CharEq>(&self, search: C) -> Option<uint>
Returns the byte index of the first character of self
that matches search
Some
containing the byte index of the last matching character or None
if there is no match
rfind
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>
Returns the byte index of the last character of self
that matches search
Some
containing the byte index of the last matching character or None
if there is no match
find_str
fn find_str(&self, needle: &str) -> Option<uint>
Returns the byte index of the first matching substring
needle
- The string to search forSome
containing the byte index of the first matching substring or None
if there is no match
repeat
fn repeat(&self, nn: uint) -> ~str
Given a string, make a new string with repeated copies of it.
slice_shift_char
fn slice_shift_char(&self) -> (char, &'self str)
Retrieves the first character from a string slice and returns it. This does not allocate a new string; instead, it returns a slice that point one character beyond the character that was shifted.
If the string does not contain any characters
lev_distance
fn lev_distance(&self, t: &str) -> uint
Levenshtein Distance between two strings.
subslice_offset
fn subslice_offset(&self, inner: &str) -> uint
Returns the byte offset of an inner slice relative to an enclosing outer slice.
Fails if inner
is not a direct slice contained within self.
let string = "a\\nb\\nc";
let mut lines = ~[];
for line in string.line_iter() { lines.push(line) }
assert!(string.subslice_offset(lines[0]) == 0); // &"a"
assert!(string.subslice_offset(lines[1]) == 2); // &"b"
assert!(string.subslice_offset(lines[2]) == 4); // &"c"
as_imm_buf
fn as_imm_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T
Work with the byte buffer and length of a slice.
The buffer does not have a null terminator.
OwnedStr
for ~str
push_str_no_overallocate
fn push_str_no_overallocate(&mut self, rhs: &str)
Appends a string slice to the back of a string, without overallocating
push_str
fn push_str(&mut self, rhs: &str)
Appends a string slice to the back of a string
push_char
fn push_char(&mut self, c: char)
Appends a character to the back of a string
pop_char
fn pop_char(&mut self) -> char
Remove the final character from a string and return it
If the string does not contain any characters
shift_char
fn shift_char(&mut self) -> char
Remove the first character from a string and return it
If the string does not contain any characters
unshift_char
fn unshift_char(&mut self, ch: char)
Prepend a char to a string
append
fn append(self, rhs: &str) -> ~str
Concatenate two strings together.
reserve
fn reserve(&mut self, n: uint)
Reserves capacity for exactly n
bytes in the given string.
Assuming single-byte characters, the resulting string will be large enough to hold a string of length n
.
If the capacity for s
is already equal to or greater than the requested capacity, then no action is taken.
reserve_at_least
fn reserve_at_least(&mut self, n: uint)
Reserves capacity for at least n
bytes in the given string.
Assuming single-byte characters, the resulting string will be large enough to hold a string of length n
.
This function will over-allocate in order to amortize the allocation costs in scenarios where the caller may need to repeatedly reserve additional space.
If the capacity for s
is already equal to or greater than the requested capacity, then no action is taken.
capacity
fn capacity(&self) -> uint
Returns the number of single-byte characters the string can hold without reallocating
truncate
fn truncate(&mut self, len: uint)
Shorten a string to the specified length (which must be <= the current length)
into_bytes
fn into_bytes(self) -> ~[u8]
Consumes the string, returning the underlying byte buffer.
The buffer does not have a null terminator.
as_mut_buf
fn as_mut_buf<T>(&mut self, f: &fn(*mut u8, uint) -> T) -> T
Clone
for ~str
clone
fn clone(&self) -> ~str
DeepClone
for ~str
deep_clone
fn deep_clone(&self) -> ~str
Clone
for @str
clone
fn clone(&self) -> @str
DeepClone
for @str
deep_clone
fn deep_clone(&self) -> @str
FromIterator<char>
for ~str
from_iterator
fn from_iterator<T: Iterator<char>>(iterator: &mut T) -> ~str
Extendable<char>
for ~str
extend
fn extend<T: Iterator<char>>(&mut self, iterator: &mut T)
Default
for &'self str
where <'self>
default
fn default() -> &'self str
Default
for ~str
default
fn default() -> ~str
Default
for @str
default
fn default() -> @str
eq
fn eq(a: &~str, b: &~str) -> bool
Bytewise string equality
eq_slice
fn eq_slice(a: &str, b: &str) -> bool
Bytewise slice equality
from_byte
fn from_byte(b: u8) -> ~str
Convert a byte to a UTF-8 string
Fails if invalid UTF-8
from_char
fn from_char(ch: char) -> ~str
Convert a char to a string
from_chars
fn from_chars(chs: &[char]) -> ~str
Convert a vector of chars to a string
from_utf16
fn from_utf16(v: &[u16]) -> ~str
Allocates a new string from the utf-16 slice provided
from_utf8
fn from_utf8(vv: &[u8]) -> ~str
Convert a vector of bytes to a new UTF-8 string
Raises the not_utf8
condition if invalid UTF-8
from_utf8_opt
fn from_utf8_opt(vv: &[u8]) -> Option<~str>
Convert a vector of bytes to a new UTF-8 string, if possible. Returns None if the vector contains invalid UTF-8.
from_utf8_owned
fn from_utf8_owned(vv: ~[u8]) -> ~str
Consumes a vector of bytes to create a new utf-8 string
Raises the not_utf8
condition if invalid UTF-8
from_utf8_owned_opt
fn from_utf8_owned_opt(vv: ~[u8]) -> Option<~str>
Consumes a vector of bytes to create a new utf-8 string. Returns None if the vector contains invalid UTF-8.
from_utf8_slice
fn from_utf8_slice<'a>(v: &'a [u8]) -> &'a str
Converts a vector to a string slice without performing any allocations.
Once the slice has been validated as utf-8, it is transmuted in-place and returned as a '&str' instead of a '&[u8]'
Fails if invalid UTF-8
from_utf8_slice_opt
fn from_utf8_slice_opt<'a>(v: &'a [u8]) -> Option<&'a str>
Converts a vector to a string slice without performing any allocations.
Returns None if the slice is not utf-8.
is_utf16
fn is_utf16(v: &[u16]) -> bool
Determines if a vector of u16
contains valid UTF-16
is_utf8
fn is_utf8(v: &[u8]) -> bool
Determines if a vector of bytes contains valid UTF-8
replace
fn replace(s: &str, from: &str, to: &str) -> ~str
Replace all occurrences of one string with another
The original string with all occurances of from
replaced with to
utf16_chars
fn utf16_chars(v: &[u16], f: &fn(char))
Iterates over the utf-16 characters in the specified slice, yielding each decoded unicode character to the function provided.
utf8_char_width
fn utf8_char_width(b: u8) -> uint
Given a first byte, determine how many bytes are in this UTF-8 character
with_capacity
fn with_capacity(capacity: uint) -> ~str
Allocates a new string with the specified capacity. The string returned is the empty string, but has capacity for much more.