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.

Type 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).

Type 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.

Type ByteRevIterator

type ByteRevIterator<'self> = Invert<ByteIterator<'self>>

External iterator for a string's bytes in reverse order. Use with the std::iterator module.

Type 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.

Type 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.

Type CharRevIterator

type CharRevIterator<'self> = Invert<CharIterator<'self>>

External iterator for a string's characters in reverse order. Use with the std::iterator module.

Type 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

Struct 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.

Struct 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.

Struct CharRange

pub struct CharRange {
    ch: char,
    next: uint,
}

Struct 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.

Struct 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.

Struct 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

Struct 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

Trait CharEq

Something that can be used to compare against a character

Method matches

fn matches(&self, char) -> bool

Determine if the splitter should split at the given character

Method only_ascii

fn only_ascii(&self) -> bool

Indicate if this is only concerned about ASCII characters, which can allow for a faster implementation.

Trait OwnedStr

Method push_str_no_overallocate

fn push_str_no_overallocate(&mut self, rhs: &str)

Method push_str

fn push_str(&mut self, rhs: &str)

Method push_char

fn push_char(&mut self, c: char)

Method pop_char

fn pop_char(&mut self) -> char

Method shift_char

fn shift_char(&mut self) -> char

Method unshift_char

fn unshift_char(&mut self, ch: char)

Method append

fn append(self, rhs: &str) -> ~str

Method reserve

fn reserve(&mut self, n: uint)

Method reserve_at_least

fn reserve_at_least(&mut self, n: uint)

Method capacity

fn capacity(&self) -> uint

Method truncate

fn truncate(&mut self, len: uint)

Method into_bytes

fn into_bytes(self) -> ~[u8]

Method 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!

Trait Str

Any string that can be represented as a slice

Method as_slice

fn as_slice<'a>(&'a self) -> &'a str

Work with self as a slice.

Method into_owned

fn into_owned(self) -> ~str

Convert self into a ~str, not making a copy if possible

Trait StrSlice

Method contains

fn contains<'a>(&self, needle: &'a str) -> bool

Method contains_char

fn contains_char(&self, needle: char) -> bool

Method iter

fn iter(&self) -> CharIterator<'self>

Method rev_iter

fn rev_iter(&self) -> CharRevIterator<'self>

Method byte_iter

fn byte_iter(&self) -> ByteIterator<'self>

Method byte_rev_iter

fn byte_rev_iter(&self) -> ByteRevIterator<'self>

Method char_offset_iter

fn char_offset_iter(&self) -> CharOffsetIterator<'self>

Method char_offset_rev_iter

fn char_offset_rev_iter(&self) -> CharOffsetRevIterator<'self>

Method split_iter

fn split_iter<Sep: CharEq>(&self, sep: Sep) -> CharSplitIterator<'self, Sep>

Method splitn_iter

fn splitn_iter<Sep: CharEq>(&self, sep: Sep, count: uint) ->
 CharSplitNIterator<'self, Sep>

Method split_terminator_iter

fn split_terminator_iter<Sep: CharEq>(&self, sep: Sep) ->
 CharSplitIterator<'self, Sep>

Method rsplit_iter

fn rsplit_iter<Sep: CharEq>(&self, sep: Sep) -> CharRSplitIterator<'self, Sep>

Method rsplitn_iter

fn rsplitn_iter<Sep: CharEq>(&self, sep: Sep, count: uint) ->
 CharSplitNIterator<'self, Sep>

Method matches_index_iter

fn matches_index_iter(&self, sep: &'self str) -> MatchesIndexIterator<'self>

Method split_str_iter

fn split_str_iter(&self, &'self str) -> StrSplitIterator<'self>

Method line_iter

fn line_iter(&self) -> CharSplitIterator<'self, char>

Method any_line_iter

fn any_line_iter(&self) -> AnyLineIterator<'self>

Method word_iter

fn word_iter(&self) -> WordIterator<'self>

Method nfd_iter

fn nfd_iter(&self) -> NormalizationIterator<'self>

Method nfkd_iter

fn nfkd_iter(&self) -> NormalizationIterator<'self>

Method ends_with

fn ends_with(&self, needle: &str) -> bool

Method is_whitespace

fn is_whitespace(&self) -> bool

Method is_alphanumeric

fn is_alphanumeric(&self) -> bool

Method char_len

fn char_len(&self) -> uint

Method slice

fn slice(&self, begin: uint, end: uint) -> &'self str

Method slice_from

fn slice_from(&self, begin: uint) -> &'self str

Method slice_to

fn slice_to(&self, end: uint) -> &'self str

Method slice_chars

fn slice_chars(&self, begin: uint, end: uint) -> &'self str

Method starts_with

fn starts_with(&self, needle: &str) -> bool

Method escape_default

fn escape_default(&self) -> ~str

Method escape_unicode

fn escape_unicode(&self) -> ~str

Method trim

fn trim(&self) -> &'self str

Method trim_left

fn trim_left(&self) -> &'self str

Method trim_right

fn trim_right(&self) -> &'self str

Method trim_chars

fn trim_chars<C: CharEq>(&self, to_trim: &C) -> &'self str

Method trim_left_chars

fn trim_left_chars<C: CharEq>(&self, to_trim: &C) -> &'self str

Method trim_right_chars

fn trim_right_chars<C: CharEq>(&self, to_trim: &C) -> &'self str

Method replace

fn replace(&self, from: &str, to: &str) -> ~str

Method to_owned

fn to_owned(&self) -> ~str

Method to_managed

fn to_managed(&self) -> @str

Method to_utf16

fn to_utf16(&self) -> ~[u16]

Method is_char_boundary

fn is_char_boundary(&self, index: uint) -> bool

Method char_range_at

fn char_range_at(&self, start: uint) -> CharRange

Method char_at

fn char_at(&self, i: uint) -> char

Method char_range_at_reverse

fn char_range_at_reverse(&self, start: uint) -> CharRange

Method char_at_reverse

fn char_at_reverse(&self, i: uint) -> char

Method as_bytes

fn as_bytes(&self) -> &'self [u8]

Method find

fn find<C: CharEq>(&self, search: C) -> Option<uint>

Method rfind

fn rfind<C: CharEq>(&self, search: C) -> Option<uint>

Method find_str

fn find_str(&self, &str) -> Option<uint>

Method repeat

fn repeat(&self, nn: uint) -> ~str

Method slice_shift_char

fn slice_shift_char(&self) -> (char, &'self str)

Method lev_distance

fn lev_distance(&self, t: &str) -> uint

Method subslice_offset

fn subslice_offset(&self, inner: &str) -> uint

Method as_imm_buf

fn as_imm_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T

Trait StrVector

Method concat

fn concat(&self) -> ~str

Method connect

fn connect(&self, sep: &str) -> ~str

Implementation of ToStr for ~str

Method to_str

fn to_str(&self) -> ~str

Implementation of ToStr for &'self str where <'self>

Method to_str

fn to_str(&self) -> ~str

Implementation of ToStr for @str

Method to_str

fn to_str(&self) -> ~str

Implementation of StrVector for &'self [S] where <'self, S: Str>

Method concat

fn concat(&self) -> ~str

Concatenate a vector of strings.

Method connect

fn connect(&self, sep: &str) -> ~str

Concatenate a vector of strings, placing a given separator between each.

Implementation of CharEq for char

Method matches

fn matches(&self, c: char) -> bool

Method only_ascii

fn only_ascii(&self) -> bool

Implementation of CharEq for &'self fn(char) -> bool where <'self>

Method matches

fn matches(&self, c: char) -> bool

Method only_ascii

fn only_ascii(&self) -> bool

Implementation of CharEq for extern "Rust" fn(char) -> bool

Method matches

fn matches(&self, c: char) -> bool

Method only_ascii

fn only_ascii(&self) -> bool

Implementation of CharEq for &'self [C] where <'self, C: CharEq>

Method matches

fn matches(&self, c: char) -> bool

Method only_ascii

fn only_ascii(&self) -> bool

Implementation of ::std::clone::Clone for CharIterator<'self> where <'self>

Automatically derived.

Method clone

fn clone(&self) -> CharIterator<'self>

Implementation of Iterator<char> for CharIterator<'self> where <'self>

Method next

fn next(&mut self) -> Option<char>

Method size_hint

fn size_hint(&self) -> (uint, Option<uint>)

Implementation of DoubleEndedIterator<char> for CharIterator<'self> where <'self>

Method next_back

fn next_back(&mut self) -> Option<char>

Implementation of ::std::clone::Clone for CharOffsetIterator<'self> where <'self>

Automatically derived.

Method clone

fn clone(&self) -> CharOffsetIterator<'self>

Implementation of Iterator<(uint, char)> for CharOffsetIterator<'self> where <'self>

Method next

fn next(&mut self) -> Option<(uint, char)>

Method size_hint

fn size_hint(&self) -> (uint, Option<uint>)

Implementation of DoubleEndedIterator<(uint, char)> for CharOffsetIterator<'self> where <'self>

Method next_back

fn next_back(&mut self) -> Option<(uint, char)>

Implementation of ::std::clone::Clone for CharSplitIterator<'self, Sep> where <'self, Sep: ::std::clone::Clone>

Automatically derived.

Method clone

fn clone(&self) -> CharSplitIterator<'self, Sep>

Implementation of ::std::clone::Clone for CharSplitNIterator<'self, Sep> where <'self, Sep: ::std::clone::Clone>

Automatically derived.

Method clone

fn clone(&self) -> CharSplitNIterator<'self, Sep>

Implementation of Iterator<&'self str> for CharSplitIterator<'self, Sep> where <'self, Sep: CharEq>

Method next

fn next(&mut self) -> Option<&'self str>

Implementation of DoubleEndedIterator<&'self str> for CharSplitIterator<'self, Sep> where <'self, Sep: CharEq>

Method next_back

fn next_back(&mut self) -> Option<&'self str>

Implementation of Iterator<&'self str> for CharSplitNIterator<'self, Sep> where <'self, Sep: CharEq>

Method next

fn next(&mut self) -> Option<&'self str>

Implementation of ::std::clone::Clone for MatchesIndexIterator<'self> where <'self>

Automatically derived.

Method clone

fn clone(&self) -> MatchesIndexIterator<'self>

Implementation of ::std::clone::Clone for StrSplitIterator<'self> where <'self>

Automatically derived.

Method clone

fn clone(&self) -> StrSplitIterator<'self>

Implementation of Iterator<(uint, uint)> for MatchesIndexIterator<'self> where <'self>

Method next

fn next(&mut self) -> Option<(uint, uint)>

Implementation of Iterator<&'self str> for StrSplitIterator<'self> where <'self>

Method next

fn next(&mut self) -> Option<&'self str>

Implementation of ::std::clone::Clone for NormalizationForm

Automatically derived.

Method clone

fn clone(&self) -> NormalizationForm

Implementation of ::std::clone::Clone for NormalizationIterator<'self> where <'self>

Automatically derived.

Method clone

fn clone(&self) -> NormalizationIterator<'self>

Implementation of Iterator<char> for NormalizationIterator<'self> where <'self>

Method next

fn next(&mut self) -> Option<char>

Method size_hint

fn size_hint(&self) -> (uint, Option<uint>)

Implementation of Str for &'self str where <'self>

Method as_slice

fn as_slice<'a>(&'a self) -> &'a str

Method into_owned

fn into_owned(self) -> ~str

Implementation of Str for ~str where <'self>

Method as_slice

fn as_slice<'a>(&'a self) -> &'a str

Method into_owned

fn into_owned(self) -> ~str

Implementation of Str for @str where <'self>

Method as_slice

fn as_slice<'a>(&'a self) -> &'a str

Method into_owned

fn into_owned(self) -> ~str

Implementation of Container for &'self str where <'self>

Method len

fn len(&self) -> uint

Implementation of Container for ~str

Method len

fn len(&self) -> uint

Implementation of Container for @str

Method len

fn len(&self) -> uint

Implementation of Mutable for ~str

Method clear

fn clear(&mut self)

Remove all content, make the string empty

Implementation of StrSlice<'self> for &'self str where <'self>

Extension methods for strings

Method contains

fn contains<'a>(&self, needle: &'a str) -> bool

Returns true if one string contains another

Arguments

  • needle - The string to look for

Method contains_char

fn contains_char(&self, needle: char) -> bool

Returns true if a string contains a char.

Arguments

  • needle - The char to look for

Method iter

fn iter(&self) -> CharIterator<'self>

An iterator over the characters of self. Note, this iterates over unicode code-points, not unicode graphemes.

Example

let v: ~[char] = "abc åäö".iter().collect();
assert_eq!(v, ~['a', 'b', 'c', ' ', 'å', 'ä', 'ö']);

Method rev_iter

fn rev_iter(&self) -> CharRevIterator<'self>

An iterator over the characters of self, in reverse order.

Method byte_iter

fn byte_iter(&self) -> ByteIterator<'self>

An iterator over the bytes of self

Method byte_rev_iter

fn byte_rev_iter(&self) -> ByteRevIterator<'self>

An iterator over the bytes of self, in reverse order

Method char_offset_iter

fn char_offset_iter(&self) -> CharOffsetIterator<'self>

An iterator over the characters of self and their byte offsets.

Method 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.

Method 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.

Example

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"]);

Method 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.

Method 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).

Example

let v: ~[&str] = "A.B.".split_terminator_iter('.').collect();
assert_eq!(v, ~["A", "B"]);

Method 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

Example

let v: ~[&str] = "Mary had a little lamb".rsplit_iter(' ').collect();
assert_eq!(v, ~["lamb", "little", "a", "had", "Mary"]);

Method 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.

Method 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.

Method split_str_iter

fn split_str_iter(&self, sep: &'self str) -> StrSplitIterator<'self>

An iterator over the substrings of self separated by sep.

Example

let v: ~[&str] = "abcXXXabcYYYabc".split_str_iter("abc").collect()
assert_eq!(v, ["", "XXX", "YYY", ""]);

Method line_iter

fn line_iter(&self) -> CharSplitIterator<'self, char>

An iterator over the lines of a string (subsequences separated by \\n).

Method 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).

Method word_iter

fn word_iter(&self) -> WordIterator<'self>

An iterator over the words of a string (subsequences separated by any sequence of whitespace).

Method nfd_iter

fn nfd_iter(&self) -> NormalizationIterator<'self>

Returns the string in Unicode Normalization Form D (canonical decomposition)

Method nfkd_iter

fn nfkd_iter(&self) -> NormalizationIterator<'self>

Returns the string in Unicode Normalization Form KD (compatibility decomposition)

Method is_whitespace

fn is_whitespace(&self) -> bool

Returns true if the string contains only whitespace

Whitespace characters are determined by char::is_whitespace

Method is_alphanumeric

fn is_alphanumeric(&self) -> bool

Returns true if the string contains only alphanumerics

Alphanumeric characters are determined by char::is_alphanumeric

Method char_len

fn char_len(&self) -> uint

Returns the number of characters that a string holds

Method 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

Method 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.

Method 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.

Method 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.

Method starts_with

fn starts_with<'a>(&self, needle: &'a str) -> bool

Returns true if needle is a prefix of the string.

Method ends_with

fn ends_with(&self, needle: &str) -> bool

Returns true if needle is a suffix of the string.

Method escape_default

fn escape_default(&self) -> ~str

Escape each char in s with char::escape_default.

Method escape_unicode

fn escape_unicode(&self) -> ~str

Escape each char in s with char::escape_unicode.

Method trim

fn trim(&self) -> &'self str

Returns a string with leading and trailing whitespace removed

Method trim_left

fn trim_left(&self) -> &'self str

Returns a string with leading whitespace removed

Method trim_right

fn trim_right(&self) -> &'self str

Returns a string with trailing whitespace removed

Method trim_chars

fn trim_chars<C: CharEq>(&self, to_trim: &C) -> &'self str

Returns a string with characters that match to_trim removed.

Arguments

  • to_trim - a character matcher

Example

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")

Method trim_left_chars

fn trim_left_chars<C: CharEq>(&self, to_trim: &C) -> &'self str

Returns a string with leading chars_to_trim removed.

Arguments

  • to_trim - a character matcher

Example

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")

Method trim_right_chars

fn trim_right_chars<C: CharEq>(&self, to_trim: &C) -> &'self str

Returns a string with trailing chars_to_trim removed.

Arguments

  • to_trim - a character matcher

Example

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")

Method replace

fn replace(&self, from: &str, to: &str) -> ~str

Replace all occurrences of one string with another

Arguments

  • from - The string to replace
  • to - The replacement string

Return value

The original string with all occurances of from replaced with to

Method to_owned

fn to_owned(&self) -> ~str

Copy a slice into a new unique str

Method to_managed

fn to_managed(&self) -> @str

Method to_utf16

fn to_utf16(&self) -> ~[u16]

Converts to a vector of u16 encoded as UTF-16.

Method 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.

Method 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.

Example

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;
}

Example output

0: 中
3: 华
6: V
7: i
8: ệ
11: t
12:
13: N
14: a
15: m

Arguments

  • s - The string
  • i - The byte offset of the char to extract

Return value

A record {ch: char, next: uint} containing the char value and the byte index of the next unicode character.

Failure

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.

Method char_at

fn char_at(&self, i: uint) -> char

Plucks the character starting at the ith byte of a string

Method 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.

Method char_at_reverse

fn char_at_reverse(&self, i: uint) -> char

Plucks the character ending at the ith byte of a string

Method as_bytes

fn as_bytes(&self) -> &'self [u8]

Work with the byte buffer of a string as a byte slice.

Method find

fn find<C: CharEq>(&self, search: C) -> Option<uint>

Returns the byte index of the first character of self that matches search

Return value

Some containing the byte index of the last matching character or None if there is no match

Method rfind

fn rfind<C: CharEq>(&self, search: C) -> Option<uint>

Returns the byte index of the last character of self that matches search

Return value

Some containing the byte index of the last matching character or None if there is no match

Method find_str

fn find_str(&self, needle: &str) -> Option<uint>

Returns the byte index of the first matching substring

Arguments

  • needle - The string to search for

Return value

Some containing the byte index of the first matching substring or None if there is no match

Method repeat

fn repeat(&self, nn: uint) -> ~str

Given a string, make a new string with repeated copies of it.

Method 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.

Failure

If the string does not contain any characters

Method lev_distance

fn lev_distance(&self, t: &str) -> uint

Levenshtein Distance between two strings.

Method 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.

Example

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"

Method 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.

Implementation of OwnedStr for ~str

Method 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

Method push_str

fn push_str(&mut self, rhs: &str)

Appends a string slice to the back of a string

Method push_char

fn push_char(&mut self, c: char)

Appends a character to the back of a string

Method pop_char

fn pop_char(&mut self) -> char

Remove the final character from a string and return it

Failure

If the string does not contain any characters

Method shift_char

fn shift_char(&mut self) -> char

Remove the first character from a string and return it

Failure

If the string does not contain any characters

Method unshift_char

fn unshift_char(&mut self, ch: char)

Prepend a char to a string

Method append

fn append(self, rhs: &str) -> ~str

Concatenate two strings together.

Method 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.

Arguments

  • s - A string
  • n - The number of bytes to reserve space for

Method 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.

Arguments

  • s - A string
  • n - The number of bytes to reserve space for

Method capacity

fn capacity(&self) -> uint

Returns the number of single-byte characters the string can hold without reallocating

Method truncate

fn truncate(&mut self, len: uint)

Shorten a string to the specified length (which must be <= the current length)

Method into_bytes

fn into_bytes(self) -> ~[u8]

Consumes the string, returning the underlying byte buffer.

The buffer does not have a null terminator.

Method as_mut_buf

fn as_mut_buf<T>(&mut self, f: &fn(*mut u8, uint) -> T) -> T

Implementation of Clone for ~str

Method clone

fn clone(&self) -> ~str

Implementation of DeepClone for ~str

Method deep_clone

fn deep_clone(&self) -> ~str

Implementation of Clone for @str

Method clone

fn clone(&self) -> @str

Implementation of DeepClone for @str

Method deep_clone

fn deep_clone(&self) -> @str

Implementation of FromIterator<char> for ~str

Method from_iterator

fn from_iterator<T: Iterator<char>>(iterator: &mut T) -> ~str

Implementation of Extendable<char> for ~str

Method extend

fn extend<T: Iterator<char>>(&mut self, iterator: &mut T)

Implementation of Default for &'self str where <'self>

Method default

fn default() -> &'self str

Implementation of Default for ~str

Method default

fn default() -> ~str

Implementation of Default for @str

Method default

fn default() -> @str

Function eq

fn eq(a: &~str, b: &~str) -> bool

Bytewise string equality

Function eq_slice

fn eq_slice(a: &str, b: &str) -> bool

Bytewise slice equality

Function from_byte

fn from_byte(b: u8) -> ~str

Convert a byte to a UTF-8 string

Failure

Fails if invalid UTF-8

Function from_char

fn from_char(ch: char) -> ~str

Convert a char to a string

Function from_chars

fn from_chars(chs: &[char]) -> ~str

Convert a vector of chars to a string

Function from_utf16

fn from_utf16(v: &[u16]) -> ~str

Allocates a new string from the utf-16 slice provided

Function from_utf8

fn from_utf8(vv: &[u8]) -> ~str

Convert a vector of bytes to a new UTF-8 string

Failure

Raises the not_utf8 condition if invalid UTF-8

Function 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.

Function from_utf8_owned

fn from_utf8_owned(vv: ~[u8]) -> ~str

Consumes a vector of bytes to create a new utf-8 string

Failure

Raises the not_utf8 condition if invalid UTF-8

Function 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.

Function 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]'

Failure

Fails if invalid UTF-8

Function 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.

Function is_utf16

fn is_utf16(v: &[u16]) -> bool

Determines if a vector of u16 contains valid UTF-16

Function is_utf8

fn is_utf8(v: &[u8]) -> bool

Determines if a vector of bytes contains valid UTF-8

Function replace

fn replace(s: &str, from: &str, to: &str) -> ~str

Replace all occurrences of one string with another

Arguments

Return value

The original string with all occurances of from replaced with to

Function 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.

Failures

Function utf8_char_width

fn utf8_char_width(b: u8) -> uint

Given a first byte, determine how many bytes are in this UTF-8 character

Function 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.