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 std::rope.
CharRange
OwnedStr
StrSlice
of ToStr for ~str
of ToStr for &'self str where <'self>
of ToStr for @str
of TotalOrd for &'self str where <'self>
of TotalOrd for ~str
of TotalOrd for @str
of Eq for &'self str where <'self>
of Eq for ~str
of Eq for @str
of TotalEq for &'self str where <'self>
of TotalEq for ~str
of TotalEq for @str
of Ord for ~str
of Ord for &'self str where <'self>
of Ord for @str
of Equiv<~str> for &'self str where <'self>
of StrSlice<'self> for &'self str where <'self>
- Extension methods for stringsof OwnedStr for ~str
of Clone for ~str
all
- Return true if a predicate matches all characters or if the string contains no charactersall_between
- Loop through a substring, char by charany
- Return true if a predicate matches any character (and false if it matches none or there are no characters)any_between
- Loop through a substring, char by charappend
- Concatenate two strings togetheras_buf
- Work with the byte buffer and length of a slice.as_bytes
- Work with the byte buffer of a string.as_bytes_slice
- Work with the byte buffer of a string as a byte slice.as_c_str
- Work with the byte buffer of a string as a null-terminated C string.byte_slice
- Work with the string as a byte slice, not including trailing null.capacity
- Returns the number of single-byte characters the string can hold without reallocatingchar_at
- Plucks the n
th character from the beginning of a stringchar_at_reverse
- Plucks the n
th character from the end of a stringchar_len
- Returns the number of characters that a string holdschar_range_at
- Pluck a character out of a string and return the index of the next character.char_range_at_reverse
- Given a byte position and a str, return the previous char and its positionconcat
- Concatenate a vector of stringsconnect
- Concatenate a vector of strings, placing a given separator between eachconnect_slices
- Concatenate a vector of strings, placing a given separator between eachcontains
- Returns true if one string contains anothercontains_char
- Returns true if a string contains a char.count_bytes
- Counts the number of bytes taken by the n
in s
starting from start
.count_chars
- As char_len but for a slice of a stringeach
- Iterate over the bytes in a stringeach_char
- Iterate over each char of a string, without allocatingeach_char_reverse
- Iterates over the chars in a string in reverseeach_chari
- Iterates over the chars in a string, with indiceseach_chari_reverse
each_line
- Splits a string into substrings separated by LF ('\n').each_line_any
- Splits a string into substrings separated by LF ('\n') and/or CR LF ("\r\n")each_reverse
- Iterate over the bytes in a string in reverseeach_split
- Splits a string into substrings using a character functioneach_split_char
- Splits a string into substrings at each occurrence of a given charactereach_split_char_no_trailing
- Like each_split_char
, but a trailing empty string is omittedeach_split_char_nonempty
- Like each_split_char
, but omits empty stringseach_split_no_trailing
- Like each_split
, but a trailing empty string is omittedeach_split_nonempty
- Like each_split
, but omits empty stringseach_split_str
- Splits a string into a vector of the substrings separated by a given stringeach_split_str_nonempty
each_split_within
- Splits a string into substrings with possibly internal whitespace, * each of them at most lim
bytes longeach_splitn
- Splits a string into substrings using a character function, cutting at most count
times.each_splitn_char
- Splits a string into substrings at each occurrence of a given character up to 'count' times.each_word
- Splits a string into substrings separated by whitespaceeachi
- Iterate over the bytes in a string, with indiceseachi_reverse
- Iterate over the bytes in a string in reverse, with indicesends_with
- Returns true if one string ends with anothereq
- Bytewise string equalityeq_slice
- Bytewise slice equalityescape_default
- Escape each char in s
with char::escape_default.escape_unicode
- Escape each char in s
with char::escape_unicode.find
- Returns the byte index of the first character that satisfies the given predicatefind_between
- Returns the byte index of the first character that satisfies the given predicate, within a given rangefind_char
- Returns the byte index of the first matching characterfind_char_between
- Returns the byte index of the first matching character within a given rangefind_char_from
- Returns the byte index of the first matching character beginning from a given byte offsetfind_from
- Returns the byte index of the first character that satisfies the given predicate, beginning from a given byte offsetfind_str
- Returns the byte index of the first matching substringfind_str_between
- Returns the byte index of the first matching substring within a given rangefind_str_from
- Returns the byte index of the first matching substring beginning from a given byte offsetfrom_byte
- Convert a byte to a UTF-8 stringfrom_bytes
- Convert a vector of bytes to a UTF-8 stringfrom_char
- Convert a char to a stringfrom_chars
- Convert a vector of chars to a stringfrom_slice
- Copy a slice into a new unique strfrom_utf16
is_ascii
- Determines if a string contains only ASCII charactersis_char_boundary
- Returns false if the index points into the middle of a multi-byte character sequence.is_empty
- Returns true if the string has length 0is_utf16
- Determines if a vector of u16
contains valid UTF-16is_utf8
- Determines if a vector of bytes contains valid UTF-8is_whitespace
- Returns true if the string contains only whitespacele
- Bytewise less than or equallen
- Returns the string length/size in bytes not counting the null terminatorlevdistance
- Levenshtein Distance between two stringsmap
- Apply a function to each characterpop_char
- Remove the final character from a string and return itpush_char
- Appends a character at the end of a stringpush_str
- Appends a string slice to the back of a stringpush_str_no_overallocate
- Appends a string slice to the back of a string, without overallocatingrepeat
- Given a string, make a new string with repeated copies of itreplace
- Replace all occurrences of one string with anotherreserve
- Reserves capacity for exactly n
bytes in the given string, not including the null terminator.reserve_at_least
- Reserves capacity for at least n
bytes in the given string, not including the null terminator.rfind
- Returns the byte index of the last character that satisfies the given predicaterfind_between
- Returns the byte index of the last character that satisfies the given predicate, within a given rangerfind_char
- Returns the byte index of the last matching characterrfind_char_between
- Returns the byte index of the last matching character within a given rangerfind_char_from
- Returns the byte index of the last matching character beginning from a given byte offsetrfind_from
- Returns the byte index of the last character that satisfies the given predicate, beginning from a given byte offsetshift_char
- Remove the first character from a string and return itslice
- Returns a slice of the given string from the byte range [begin
..end
)slice_shift_char
- Removes the first character from a string slice and returns itstarts_with
- Returns true if one string starts with anothersubstr
- Take a substring of another.to_bytes
- Converts a string to a unique vector of bytesto_chars
- Convert a string to a unique vector of charactersto_lower
- Convert a string to lowercaseto_upper
- Convert a string to uppercaseto_utf16
- Converts to a vector of u16
encoded as UTF-16trim
- Returns a string with leading and trailing whitespace removedtrim_chars
- Returns a string with leading and trailing chars_to_trim
removed.trim_left
- Returns a string with leading whitespace removedtrim_left_chars
- Returns a string with leading chars_to_trim
removed.trim_right
- Returns a string with trailing whitespace removedtrim_right_chars
- Returns a string with trailing chars_to_trim
removed.unshift_char
- Prepend a char to a stringutf16_chars
utf8_char_width
- Given a first byte, determine how many bytes are in this UTF-8 characterwith_capacity
str::raw
- Unsafe operationsstr::traits
CharRange
pub struct CharRange {
ch: char,
next: uint,
}
OwnedStr
push_str
fn push_str(&mut self, v: &str)
push_char
fn push_char(&mut self, c: char)
StrSlice
all
fn all(&self, it: &fn(char) -> bool) -> bool
any
fn any(&self, it: &fn(char) -> bool) -> bool
contains
fn contains<'a>(&self, needle: &'a str) -> bool
contains_char
fn contains_char(&self, needle: char) -> bool
each
fn each(&self, it: &fn(u8) -> bool)
eachi
fn eachi(&self, it: &fn(uint, u8) -> bool)
each_reverse
fn each_reverse(&self, it: &fn(u8) -> bool)
eachi_reverse
fn eachi_reverse(&self, it: &fn(uint, u8) -> bool)
each_char
fn each_char(&self, it: &fn(char) -> bool)
each_chari
fn each_chari(&self, it: &fn(uint, char) -> bool)
each_char_reverse
fn each_char_reverse(&self, it: &fn(char) -> bool)
each_chari_reverse
fn each_chari_reverse(&self, it: &fn(uint, char) -> bool)
ends_with
fn ends_with(&self, needle: &str) -> bool
is_empty
fn is_empty(&self) -> bool
is_whitespace
fn is_whitespace(&self) -> bool
is_alphanumeric
fn is_alphanumeric(&self) -> bool
len
fn len(&self) -> uint
char_len
fn char_len(&self) -> uint
slice
fn slice(&self, begin: uint, end: uint) -> &'self str
each_split
fn each_split(&self, sepfn: &fn(char) -> bool, it: &fn(&'self str) -> bool)
each_split_char
fn each_split_char(&self, sep: char, it: &fn(&'self str) -> bool)
each_split_str
fn each_split_str<'a>(&self, sep: &'a str, it: &fn(&'self str) -> bool)
starts_with
fn starts_with<'a>(&self, needle: &'a str) -> bool
substr
fn substr(&self, begin: uint, n: uint) -> &'self str
to_lower
fn to_lower(&self) -> ~str
to_upper
fn to_upper(&self) -> ~str
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(&self, chars_to_trim: &[char]) -> &'self str
trim_left_chars
fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str
trim_right_chars
fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str
to_owned
fn to_owned(&self) -> ~str
to_managed
fn to_managed(&self) -> @str
char_at
fn char_at(&self, i: uint) -> char
char_at_reverse
fn char_at_reverse(&self, i: uint) -> char
to_bytes
fn to_bytes(&self) -> ~[u8]
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
TotalOrd
for &'self str
where <'self>
cmp
fn cmp(&self, other: &&'self str) -> Ordering
TotalOrd
for ~str
cmp
fn cmp(&self, other: &~str) -> Ordering
TotalOrd
for @str
cmp
fn cmp(&self, other: &@str) -> Ordering
Eq
for &'self str
where <'self>
eq
fn eq(&self, other: &&'self str) -> bool
ne
fn ne(&self, other: &&'self str) -> bool
Eq
for ~str
eq
fn eq(&self, other: &~str) -> bool
ne
fn ne(&self, other: &~str) -> bool
Eq
for @str
eq
fn eq(&self, other: &@str) -> bool
ne
fn ne(&self, other: &@str) -> bool
TotalEq
for &'self str
where <'self>
equals
fn equals(&self, other: &&'self str) -> bool
TotalEq
for ~str
equals
fn equals(&self, other: &~str) -> bool
TotalEq
for @str
equals
fn equals(&self, other: &@str) -> bool
Ord
for ~str
lt
fn lt(&self, other: &~str) -> bool
le
fn le(&self, other: &~str) -> bool
ge
fn ge(&self, other: &~str) -> bool
gt
fn gt(&self, other: &~str) -> bool
Ord
for &'self str
where <'self>
lt
fn lt(&self, other: &&'self str) -> bool
le
fn le(&self, other: &&'self str) -> bool
ge
fn ge(&self, other: &&'self str) -> bool
gt
fn gt(&self, other: &&'self str) -> bool
Ord
for @str
lt
fn lt(&self, other: &@str) -> bool
le
fn le(&self, other: &@str) -> bool
ge
fn ge(&self, other: &@str) -> bool
gt
fn gt(&self, other: &@str) -> bool
Equiv<~str>
for &'self str
where <'self>
equiv
fn equiv(&self, other: &~str) -> bool
StrSlice<'self>
for &'self str
where <'self>
Extension methods for strings
all
fn all(&self, it: &fn(char) -> bool) -> bool
Return true if a predicate matches all characters or if the string contains no characters
any
fn any(&self, it: &fn(char) -> bool) -> bool
Return true if a predicate matches any character (and false if it matches none or there are no characters)
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
each
fn each(&self, it: &fn(u8) -> bool)
Iterate over the bytes in a string
eachi
fn eachi(&self, it: &fn(uint, u8) -> bool)
Iterate over the bytes in a string, with indices
each_reverse
fn each_reverse(&self, it: &fn(u8) -> bool)
Iterate over the bytes in a string
eachi_reverse
fn eachi_reverse(&self, it: &fn(uint, u8) -> bool)
Iterate over the bytes in a string, with indices
each_char
fn each_char(&self, it: &fn(char) -> bool)
Iterate over the chars in a string
each_chari
fn each_chari(&self, it: &fn(uint, char) -> bool)
Iterate over the chars in a string, with indices
each_char_reverse
fn each_char_reverse(&self, it: &fn(char) -> bool)
Iterate over the chars in a string in reverse
each_chari_reverse
fn each_chari_reverse(&self, it: &fn(uint, char) -> bool)
Iterate over the chars in a string in reverse, with indices from the end
ends_with
fn ends_with(&self, needle: &str) -> bool
Returns true if one string ends with another
is_empty
fn is_empty(&self) -> bool
Returns true if the string has length 0
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
len
fn len(&self) -> uint
Returns the size in bytes not counting the null terminator
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
each_split
fn each_split(&self, sepfn: &fn(char) -> bool, it: &fn(&'self str) -> bool)
Splits a string into substrings using a character function
each_split_char
fn each_split_char(&self, sep: char, it: &fn(&'self str) -> bool)
Splits a string into substrings at each occurrence of a given character
each_split_str
fn each_split_str<'a>(&self, sep: &'a str, it: &fn(&'self str) -> bool)
Splits a string into a vector of the substrings separated by a given string
starts_with
fn starts_with<'a>(&self, needle: &'a str) -> bool
Returns true if one string starts with another
substr
fn substr(&self, begin: uint, n: uint) -> &'self str
Take a substring of another.
Returns a string containing n
characters starting at byte offset begin
.
to_lower
fn to_lower(&self) -> ~str
Convert a string to lowercase
to_upper
fn to_upper(&self) -> ~str
Convert a string to uppercase
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(&self, chars_to_trim: &[char]) -> &'self str
trim_left_chars
fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str
trim_right_chars
fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str
to_owned
fn to_owned(&self) -> ~str
to_managed
fn to_managed(&self) -> @str
char_at
fn char_at(&self, i: uint) -> char
char_at_reverse
fn char_at_reverse(&self, i: uint) -> char
to_bytes
fn to_bytes(&self) -> ~[u8]
OwnedStr
for ~str
push_str
fn push_str(&mut self, v: &str)
push_char
fn push_char(&mut self, c: char)
Clone
for ~str
clone
fn clone(&self) -> ~str
all
fn all(s: &str, it: &fn(char) -> bool) -> bool
Return true if a predicate matches all characters or if the string contains no characters
all_between
fn all_between(s: &str, start: uint, end: uint, it: &fn(char) -> bool) -> bool
Loop through a substring, char by char
start
or end
do not represent valid positions inside s
s
. Return true
to continue, false
to stop.true
If execution proceeded correctly, false
if it was interrupted, that is if it
returned false
at any point.
any
fn any(ss: &str, pred: &fn(char) -> bool) -> bool
Return true if a predicate matches any character (and false if it matches none or there are no characters)
any_between
fn any_between(s: &str, start: uint, end: uint, it: &fn(char) -> bool) -> bool
Loop through a substring, char by char
start
or end
do not represent valid positions inside s
s
. Return true
to continue, false
to stop.true
if it
returns true
for any character
append
fn append(lhs: ~str, rhs: &str) -> ~str
Concatenate two strings together
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.
as_bytes
fn as_bytes<T>(s: &const ~str, f: &fn(&~[u8]) -> T) -> T
Work with the byte buffer of a string.
Allows for unsafe manipulation of strings, which is useful for foreign interop.
let i = str::as_bytes("Hello World") { |bytes| vec::len(bytes) };
as_bytes_slice
fn as_bytes_slice<'a>(s: &'a str) -> &'a [u8]
Work with the byte buffer of a string as a byte slice.
The byte slice does not include the null terminator.
as_c_str
fn as_c_str<T>(s: &str, 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.
let s = str::as_c_str("PATH", { |path| libc::getenv(path) });
byte_slice
fn byte_slice<T>(s: &str, f: &fn(v: &[u8]) -> T) -> T
Work with the string as a byte slice, not including trailing null.
capacity
fn capacity(s: &const ~str) -> uint
Returns the number of single-byte characters the string can hold without reallocating
char_at
fn char_at(s: &str, i: uint) -> char
Plucks the n
th character from the beginning of a string
char_at_reverse
fn char_at_reverse(s: &str, i: uint) -> char
Plucks the n
th character from the end of a string
char_len
fn char_len(s: &str) -> uint
Returns the number of characters that a string holds
char_range_at
fn char_range_at(s: &str, 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 < str::len(s) {
let CharRange {ch, next} = str::char_range_at(s, i);
std::io::println(fmt!("%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_range_at_reverse
fn char_range_at_reverse(ss: &str, 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
concat
fn concat(v: &[~str]) -> ~str
Concatenate a vector of strings
connect
fn connect(v: &[~str], sep: &str) -> ~str
Concatenate a vector of strings, placing a given separator between each
connect_slices
fn connect_slices(v: &[&str], sep: &str) -> ~str
Concatenate a vector of strings, placing a given separator between each
contains
fn contains<'a, 'b>(haystack: &'a str, needle: &'b str) -> bool
Returns true if one string contains another
contains_char
fn contains_char(haystack: &str, needle: char) -> bool
Returns true if a string contains a char.
count_bytes
fn count_bytes<'b>(s: &'b str, start: uint, n: uint) -> uint
Counts the number of bytes taken by the n
in s
starting from start
.
count_chars
fn count_chars(s: &str, start: uint, end: uint) -> uint
As char_len but for a slice of a string
s
where to start counting in bytesThe number of Unicode characters in s
between the given indices.
each
fn each(s: &str, it: &fn(u8) -> bool)
Iterate over the bytes in a string
each_char
fn each_char(s: &str, it: &fn(char) -> bool)
Iterate over each char of a string, without allocating
each_char_reverse
fn each_char_reverse(s: &str, it: &fn(char) -> bool)
Iterates over the chars in a string in reverse
each_chari
fn each_chari(s: &str, it: &fn(uint, char) -> bool)
Iterates over the chars in a string, with indices
each_chari_reverse
fn each_chari_reverse(s: &str, it: &fn(uint, char) -> bool)
each_line
fn each_line<'a>(s: &'a str, it: &fn(&'a str) -> bool)
Splits a string into substrings separated by LF ('\n').
each_line_any
fn each_line_any<'a>(s: &'a str, it: &fn(&'a str) -> bool)
Splits a string into substrings separated by LF ('\n') and/or CR LF ("\r\n")
each_reverse
fn each_reverse(s: &str, it: &fn(u8) -> bool)
Iterate over the bytes in a string in reverse
each_split
fn each_split<'a>(s: &'a str, sepfn: &fn(char) -> bool,
it: &fn(&'a str) -> bool)
Splits a string into substrings using a character function
each_split_char
fn each_split_char<'a>(s: &'a str, sep: char, it: &fn(&'a str) -> bool)
Splits a string into substrings at each occurrence of a given character
each_split_char_no_trailing
fn each_split_char_no_trailing<'a>(s: &'a str, sep: char,
it: &fn(&'a str) -> bool)
Like each_split_char
, but a trailing empty string is omitted
each_split_char_nonempty
fn each_split_char_nonempty<'a>(s: &'a str, sep: char,
it: &fn(&'a str) -> bool)
Like each_split_char
, but omits empty strings
each_split_no_trailing
fn each_split_no_trailing<'a>(s: &'a str, sepfn: &fn(char) -> bool,
it: &fn(&'a str) -> bool)
Like each_split
, but a trailing empty string is omitted
each_split_nonempty
fn each_split_nonempty<'a>(s: &'a str, sepfn: &fn(char) -> bool,
it: &fn(&'a str) -> bool)
Like each_split
, but omits empty strings
each_split_str
fn each_split_str<'a, 'b>(s: &'a str, sep: &'b str, it: &fn(&'a str) -> bool)
Splits a string into a vector of the substrings separated by a given string
let mut v = ~[];
for each_split_str(".XXX.YYY.", ".") |subs| { v.push(subs); }
assert!(v == ["", "XXX", "YYY", ""]);
each_split_str_nonempty
fn each_split_str_nonempty<'a,
'b>(s: &'a str, sep: &'b str,
it: &fn(&'a str) -> bool)
each_split_within
fn each_split_within<'a>(ss: &'a str, lim: uint, it: &fn(&'a str) -> 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.
each_splitn
fn each_splitn<'a>(s: &'a str, sepfn: &fn(char) -> bool, count: uint,
it: &fn(&'a str) -> bool)
Splits a string into substrings using a character function, cutting at most count
times.
each_splitn_char
fn each_splitn_char<'a>(s: &'a str, sep: char, count: uint,
it: &fn(&'a str) -> bool)
Splits a string into substrings at each occurrence of a given character up to 'count' times.
The character must be a valid UTF-8/ASCII character
each_word
fn each_word<'a>(s: &'a str, it: &fn(&'a str) -> bool)
Splits a string into substrings separated by whitespace
eachi
fn eachi(s: &str, it: &fn(uint, u8) -> bool)
Iterate over the bytes in a string, with indices
eachi_reverse
fn eachi_reverse(s: &str, it: &fn(uint, u8) -> bool)
Iterate over the bytes in a string in reverse, with indices
ends_with
fn ends_with<'a, 'b>(haystack: &'a str, needle: &'b str) -> bool
Returns true if one string ends with another
eq
fn eq(a: &~str, b: &~str) -> bool
Bytewise string equality
eq_slice
fn eq_slice(a: &str, b: &str) -> bool
Bytewise slice equality
escape_default
fn escape_default(s: &str) -> ~str
Escape each char in s
with char::escape_default.
escape_unicode
fn escape_unicode(s: &str) -> ~str
Escape each char in s
with char::escape_unicode.
find
fn find(s: &str, f: &fn(char) -> bool) -> Option<uint>
Returns the byte index of the first character that satisfies the given predicate
s
- The string to searchf
- The predicate to satisfyAn option
containing the byte index of the first matching character or none
if there is no match
find_between
fn find_between(s: &str, start: uint, end: uint, f: &fn(char) -> bool) ->
Option<uint>
Returns the byte index of the first character that satisfies the given predicate, within a given range
s
- The string to searchstart
- The byte index to begin searching at, inclusiveend
- The byte index to end searching at, exclusivef
- The predicate to satisfyAn option
containing the byte index of the first matching character or none
if there is no match
start
must be less than or equal to end
and end
must be less than or equal to len(s)
. start
must be the index of a character boundary, as defined by is_char_boundary
.
find_char
fn find_char(s: &str, c: char) -> Option<uint>
Returns the byte index of the first matching character
s
- The string to searchc
- The character to search forAn option
containing the byte index of the first matching character or none
if there is no match
find_char_between
fn find_char_between(s: &str, c: char, start: uint, end: uint) -> Option<uint>
Returns the byte index of the first matching character within a given range
s
- The string to searchc
- The character to search forstart
- The byte index to begin searching at, inclusiveend
- The byte index to end searching at, exclusiveAn option
containing the byte index of the first matching character or none
if there is no match
start
must be less than or equal to end
and end
must be less than or equal to len(s)
. start
must be the index of a character boundary, as defined by is_char_boundary
.
find_char_from
fn find_char_from(s: &str, c: char, start: uint) -> Option<uint>
Returns the byte index of the first matching character beginning from a given byte offset
s
- The string to searchc
- The character to search forstart
- The byte index to begin searching at, inclusiveAn option
containing the byte index of the first matching character or none
if there is no match
start
must be less than or equal to len(s)
. start
must be the index of a character boundary, as defined by is_char_boundary
.
find_from
fn find_from(s: &str, start: uint, f: &fn(char) -> bool) -> Option<uint>
Returns the byte index of the first character that satisfies the given predicate, beginning from a given byte offset
s
- The string to searchstart
- The byte index to begin searching at, inclusivef
- The predicate to satisfyAn option
containing the byte index of the first matching charactor or none
if there is no match
start
must be less than or equal to len(s)
. start
must be the index of a character boundary, as defined by is_char_boundary
.
find_str
fn find_str<'a, 'b>(haystack: &'a str, needle: &'b str) -> Option<uint>
Returns the byte index of the first matching substring
haystack
- The string to searchneedle
- The string to search forAn option
containing the byte index of the first matching substring or none
if there is no match
find_str_between
fn find_str_between<'a,
'b>(haystack: &'a str, needle: &'b str, start: uint,
end: uint) -> Option<uint>
Returns the byte index of the first matching substring within a given range
haystack
- The string to searchneedle
- The string to search forstart
- The byte index to begin searching at, inclusiveend
- The byte index to end searching at, exclusiveAn option
containing the byte index of the first matching character or none
if there is no match
start
must be less than or equal to end
and end
must be less than or equal to len(s)
.
find_str_from
fn find_str_from<'a, 'b>(haystack: &'a str, needle: &'b str, start: uint) ->
Option<uint>
Returns the byte index of the first matching substring beginning from a given byte offset
haystack
- The string to searchneedle
- The string to search forstart
- The byte index to begin searching at, inclusiveAn option
containing the byte index of the last matching character or none
if there is no match
start
must be less than or equal to len(s)
from_byte
fn from_byte(b: u8) -> ~str
Convert a byte to a UTF-8 string
Fails if invalid UTF-8
from_bytes
fn from_bytes(vv: &const [u8]) -> ~str
Convert a vector of bytes 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_slice
fn from_slice(s: &str) -> ~str
Copy a slice into a new unique str
from_utf16
fn from_utf16(v: &[u16]) -> ~str
is_ascii
fn is_ascii(s: &str) -> bool
Determines if a string contains only ASCII characters
is_char_boundary
fn is_char_boundary(s: &str, index: uint) -> bool
Returns false if the index points into the middle of a multi-byte character sequence.
is_empty
fn is_empty(s: &str) -> bool
Returns true if the string has length 0
is_utf16
fn is_utf16(v: &[u16]) -> bool
Determines if a vector of u16
contains valid UTF-16
is_utf8
fn is_utf8(v: &const [u8]) -> bool
Determines if a vector of bytes contains valid UTF-8
is_whitespace
fn is_whitespace(s: &str) -> bool
Returns true if the string contains only whitespace
Whitespace characters are determined by char::is_whitespace
le
fn le(a: &str, b: &str) -> bool
Bytewise less than or equal
len
fn len(s: &str) -> uint
Returns the string length/size in bytes not counting the null terminator
levdistance
fn levdistance(s: &str, t: &str) -> uint
Levenshtein Distance between two strings
map
fn map(ss: &str, ff: &fn(char) -> char) -> ~str
Apply a function to each character
pop_char
fn pop_char(s: &mut ~str) -> char
Remove the final character from a string and return it
If the string does not contain any characters
push_char
fn push_char(s: &mut ~str, ch: char)
Appends a character at the end of a string
push_str
fn push_str(lhs: &mut ~str, rhs: &str)
Appends a string slice to the back of a string
push_str_no_overallocate
fn push_str_no_overallocate(lhs: &mut ~str, rhs: &str)
Appends a string slice to the back of a string, without overallocating
repeat
fn repeat(ss: &str, nn: uint) -> ~str
Given a string, make a new string with repeated copies of it
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
reserve
fn reserve(s: &mut ~str, 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.
reserve_at_least
fn reserve_at_least(s: &mut ~str, 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.
rfind
fn rfind(s: &str, f: &fn(char) -> bool) -> Option<uint>
Returns the byte index of the last character that satisfies the given predicate
s
- The string to searchf
- The predicate to satisfyAn option containing the byte index of the last matching character or none
if there is no match
rfind_between
fn rfind_between(s: &str, start: uint, end: uint, f: &fn(char) -> bool) ->
Option<uint>
Returns the byte index of the last character that satisfies the given predicate, within a given range
s
- The string to searchstart
- The byte index to begin searching at, exclusiveend
- The byte index to end searching at, inclusivef
- The predicate to satisfyAn option
containing the byte index of the last matching character or none
if there is no match
end
must be less than or equal to start
and start
must be less than or equal to len(s)
. start
must be the index of a character boundary, as defined by is_char_boundary
rfind_char
fn rfind_char(s: &str, c: char) -> Option<uint>
Returns the byte index of the last matching character
s
- The string to searchc
- The character to search forAn option
containing the byte index of the last matching character or none
if there is no match
rfind_char_between
fn rfind_char_between(s: &str, c: char, start: uint, end: uint) ->
Option<uint>
Returns the byte index of the last matching character within a given range
s
- The string to searchc
- The character to search forstart
- The byte index to begin searching at, exclusiveend
- The byte index to end searching at, inclusiveAn option
containing the byte index of the last matching character or none
if there is no match
end
must be less than or equal to start
and start
must be less than or equal to len(s)
. start
must be the index of a character boundary, as defined by is_char_boundary
.
rfind_char_from
fn rfind_char_from(s: &str, c: char, start: uint) -> Option<uint>
Returns the byte index of the last matching character beginning from a given byte offset
s
- The string to searchc
- The character to search forstart
- The byte index to begin searching at, exclusiveAn option
containing the byte index of the last matching character or none
if there is no match
start
must be less than or equal to len(s)
. start
must be the index of a character boundary, as defined by is_char_boundary
.
rfind_from
fn rfind_from(s: &str, start: uint, f: &fn(char) -> bool) -> Option<uint>
Returns the byte index of the last character that satisfies the given predicate, beginning from a given byte offset
s
- The string to searchstart
- The byte index to begin searching at, exclusivef
- The predicate to satisfyAn option
containing the byte index of the last matching character or none
if there is no match
start
must be less than or equal to len(s)',
startmust be the index of a character boundary, as defined by
is_char_boundary`
shift_char
fn shift_char(s: &mut ~str) -> char
Remove the first character from a string and return it
If the string does not contain any characters
slice
fn slice<'a>(s: &'a str, begin: uint, end: uint) -> &'a 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_shift_char
fn slice_shift_char<'a>(s: &'a str) -> (char, &'a str)
Removes the first character from a string slice and returns it. This does not allocate a new string; instead, it mutates a slice to point one character beyond the character that was shifted.
If the string does not contain any characters
starts_with
fn starts_with<'a, 'b>(haystack: &'a str, needle: &'b str) -> bool
Returns true if one string starts with another
substr
fn substr<'a>(s: &'a str, begin: uint, n: uint) -> &'a str
Take a substring of another.
Returns a slice pointing at n
characters starting from byte offset begin
.
to_bytes
fn to_bytes(s: &str) -> ~[u8]
Converts a string to a unique vector of bytes
The result vector is not null-terminated.
to_chars
fn to_chars(s: &str) -> ~[char]
Convert a string to a unique vector of characters
to_lower
fn to_lower(s: &str) -> ~str
Convert a string to lowercase. ASCII only
to_upper
fn to_upper(s: &str) -> ~str
Convert a string to uppercase. ASCII only
to_utf16
fn to_utf16(s: &str) -> ~[u16]
Converts to a vector of u16
encoded as UTF-16
trim
fn trim<'a>(s: &'a str) -> &'a str
Returns a string with leading and trailing whitespace removed
trim_chars
fn trim_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str
Returns a string with leading and trailing chars_to_trim
removed.
trim_left
fn trim_left<'a>(s: &'a str) -> &'a str
Returns a string with leading whitespace removed
trim_left_chars
fn trim_left_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str
Returns a string with leading chars_to_trim
removed.
trim_right
fn trim_right<'a>(s: &'a str) -> &'a str
Returns a string with trailing whitespace removed
trim_right_chars
fn trim_right_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str
Returns a string with trailing chars_to_trim
removed.
unshift_char
fn unshift_char(s: &mut ~str, ch: char)
Prepend a char to a string
utf16_chars
fn utf16_chars(v: &[u16], f: &fn(char))
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