String manipulation

Strings are a packed UTF-8 representation of text, stored as null terminated buffers of u8 bytes. Strings should be indexed in bytes, for efficiency, but UTF-8 unsafe operations should be avoided. For some heavy-duty uses, try extra::rope.

Type AnyLineIterator

type AnyLineIterator<'self> = MapIterator<'self, &'self str, &'self str, StrCharSplitIterator<'self, char>>

An iterator over the lines of a string, separated by either \\n or (\\r\\n).

Type WordIterator

type WordIterator<'self> = FilterIterator<'self,
&'self str, StrCharSplitIterator<'self, extern "Rust" fn(char) -> bool>>

An iterator over the words of a string, separated by an sequence of whitespace

Struct CharRange

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

Struct StrBytesIterator

pub struct StrBytesIterator<'self> {
    priv it: vec::VecIterator<'self, u8>,
}

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

Struct StrBytesRevIterator

pub struct StrBytesRevIterator<'self> {
    priv it: vec::VecRevIterator<'self, u8>,
}

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

Struct StrCharIterator

pub struct StrCharIterator<'self> {
    priv index: uint,
    priv string: &'self str,
}

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

Struct StrCharRevIterator

pub struct StrCharRevIterator<'self> {
    priv index: uint,
    priv string: &'self str,
}

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

Struct StrCharSplitIterator

pub struct StrCharSplitIterator<'self, Sep> {
    priv string: &'self str,
    priv position: uint,
    priv sep: Sep,
    /// The number of splits remaining
    priv count: uint,
    /// Whether an empty string at the end is allowed
    priv allow_trailing_empty: bool,
    priv finished: bool,
    priv only_ascii: bool,
}

An iterator over the substrings of a string, separated by sep.

Struct StrMatchesIndexIterator

pub struct StrMatchesIndexIterator<'self> {
    priv haystack: &'self str,
    priv needle: &'self str,
    priv position: uint,
}

An iterator over the start and end indicies of the matches of a substring within a larger string

Struct StrStrSplitIterator

pub struct StrStrSplitIterator<'self> {
    priv it: StrMatchesIndexIterator<'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 NullTerminatedStr

Method as_bytes_with_null

fn as_bytes_with_null<'a>(&'a self) -> &'a [u8]

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 as_bytes_with_null_consume

fn as_bytes_with_null_consume(self) -> ~[u8]

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.

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) -> StrCharIterator<'self>

Method rev_iter

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

Method bytes_iter

fn bytes_iter(&self) -> StrBytesIterator<'self>

Method bytes_rev_iter

fn bytes_rev_iter(&self) -> StrBytesRevIterator<'self>

Method split_iter

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

Method splitn_iter

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

Method split_options_iter

fn split_options_iter<Sep: CharEq>(&self, sep: Sep, count: uint,
                                   allow_trailing_empty: bool) ->
 StrCharSplitIterator<'self, Sep>

Method matches_index_iter

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

Method split_str_iter

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

Method line_iter

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

Method any_line_iter

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

Method word_iter

fn word_iter(&self) -> WordIterator<'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 map_chars

fn map_chars(&self, ff: &fn(char) -> char) -> ~str

Method lev_distance

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

Method subslice_offset

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

Trait StrUtil

A dummy trait to hold all the utility methods that we implement on strings.

Method as_c_str

fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T

Work with the byte buffer of a string as a null-terminated C string.

Allows for unsafe manipulation of strings, which is useful for foreign interop. This is similar to str::as_buf, but guarantees null-termination. If the given slice is not already null-terminated, this function will allocate a temporary, copy the slice, null terminate it, and pass that instead.

Example

let s = "PATH".as_c_str(|path| libc::getenv(path));

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 Iterator<&'self str> for StrCharSplitIterator<'self, Sep> where <'self, Sep: CharEq>

Method next

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

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

Method next

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

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

Method next

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

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

Method as_c_str

fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T

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

Method as_slice

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

Implementation of Str for ~str where <'self>

Method as_slice

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

Implementation of Str for @str where <'self>

Method as_slice

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

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

Method len

fn len(&self) -> uint

Method is_empty

fn is_empty(&self) -> bool

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) -> StrCharIterator<'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) -> StrCharRevIterator<'self>

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

Method bytes_iter

fn bytes_iter(&self) -> StrBytesIterator<'self>

An iterator over the bytes of self

Method bytes_rev_iter

fn bytes_rev_iter(&self) -> StrBytesRevIterator<'self>

An iterator over the bytes of self, in reverse order

Method split_iter

fn split_iter<Sep: CharEq>(&self, sep: Sep) ->
 StrCharSplitIterator<'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) ->
 StrCharSplitIterator<'self, Sep>

An iterator over substrings of self, separated by characters matched by sep, restricted to splitting at most count times.

Method split_options_iter

fn split_options_iter<Sep: CharEq>(&self, sep: Sep, count: uint,
                                   allow_trailing_empty: bool) ->
 StrCharSplitIterator<'self, Sep>

An iterator over substrings of self, separated by characters matched by sep, splitting at most count times, and possibly not including the trailing empty substring, if it exists.

Method matches_index_iter

fn matches_index_iter(&self, sep: &'self str) ->
 StrMatchesIndexIterator<'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) -> StrStrSplitIterator<'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) -> StrCharSplitIterator<'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 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);
    std::io::println(fmt!("%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.

The byte slice does not include the null terminator.

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 map_chars

fn map_chars(&self, ff: &fn(char) -> char) -> ~str

Apply a function to each character.

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 string.line_iter().advance |line| { 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"

Implementation of NullTerminatedStr for ~str

Method as_bytes_with_null

fn as_bytes_with_null<'a>(&'a self) -> &'a [u8]

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

The byte slice does include the null terminator.

Implementation of NullTerminatedStr for @str

Method as_bytes_with_null

fn as_bytes_with_null<'a>(&'a self) -> &'a [u8]

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

The byte slice does include the 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, not including the null terminator.

Assuming single-byte characters, the resulting string will be large enough to hold a string of length n. To account for the null terminator, the underlying buffer will have the size n + 1.

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, not including the null terminator.

Assuming single-byte characters, the resulting string will be large enough to hold a string of length n. To account for the null terminator, the underlying buffer will have the size n + 1.

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 as_bytes_with_null_consume

fn as_bytes_with_null_consume(self) -> ~[u8]

Convert to a vector of bytes. This does not allocate a new string, and includes the null terminator.

Implementation of Clone for ~str

Method clone

fn clone(&self) -> ~str

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

Method next

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

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

Method next

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

Implementation of Iterator<u8> for StrBytesIterator<'self> where <'self>

Method next

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

Implementation of Iterator<u8> for StrBytesRevIterator<'self> where <'self>

Method next

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

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

Method zero

fn zero() -> &'self str

Method is_zero

fn is_zero(&self) -> bool

Implementation of Zero for ~str

Method zero

fn zero() -> ~str

Method is_zero

fn is_zero(&self) -> bool

Implementation of Zero for @str

Method zero

fn zero() -> @str

Method is_zero

fn is_zero(&self) -> bool

Function as_buf

fn as_buf<T>(s: &str, f: &fn(*u8, uint) -> T) -> T

Work with the byte buffer and length of a slice.

The given length is one byte longer than the 'official' indexable length of the string. This is to permit probing the byte past the indexable area for a null byte, as is the case in slices pointing to full strings, or suffixes of them.

Function as_c_str

fn as_c_str<T>(s: &str, f: &fn(*libc::c_char) -> T) -> T

Deprecated. Use the as_c_str method on strings instead.

Function count_bytes

fn count_bytes<'b>(s: &'b str, start: uint, n: uint) -> uint

Counts the number of bytes taken by the first n chars in s starting from start.

Function count_chars

fn count_chars(s: &str, start: uint, end: uint) -> uint

As char_len but for a slice of a string

Arguments

Return value

The number of Unicode characters in s between the given indices.

Function each_split_within

fn each_split_within<'a>(ss: &'a str, lim: uint, it: &fn(&'a str) -> bool) ->
 bool

Splits a string into substrings with possibly internal whitespace, * each of them at most lim bytes long. The substrings have leading and trailing * whitespace removed, and are only cut at whitespace boundaries. * * #Failure: * * Fails during iteration if the string contains a non-whitespace * sequence longer than the limit.

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_bytes

fn from_bytes(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_bytes_owned

fn from_bytes_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_bytes_slice

fn from_bytes_slice<'a>(vector: &'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_bytes_with_null

fn from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a str

Convert a vector of bytes to a UTF-8 string. The vector needs to be one byte longer than the string, and end with a 0 byte.

Compared to from_bytes(), this fn doesn't need to allocate a new owned str.

Failure

Fails if invalid UTF-8 Fails if not null terminated

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 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 to_owned

fn to_owned(s: &str) -> ~str

Copy a slice into a new unique str

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.