Searchable
trait Searchable<T: Show> Import
Source: use text::{Searchable}; src/text.hy Pattern search methods (contains, find, split, replace_all, …) on any [Show] type, which search its display form. string has them built in, so "foobar".contains("oba") works with a string or any other [Pattern] (such as a compiled Regex).
Give your own type the methods with an empty instance; they search
value.show():
use text::{Searchable};
use tests::assert;
class Version {
pub major: int,
pub minor: int,
}
impl Show for Version {
pub fn show(Version v) -> string {
return string::format("%i.%i", v.major, v.minor);
}
}
impl Searchable for Version {}
fn main() {
assert!(new Version(1, 12).starts_with("1."));
}
Empty matches step over one character, so an empty pattern matches
between every pair of characters ("ab".split("") is ["", "a", "b", ""]).
Required methods#
| Name | Description |
|---|---|
contains | Returns true when pat matches anywhere in hay. |
starts_with | Returns true when a match of pat starts at the beginning of hay. |
ends_with | Returns true when a match of pat ends at the end of hay. |
find | Returns the byte span of the first match of pat, or None. |
find_all | Returns the byte spans of every non-overlapping match, left to right. |
split | Returns the pieces of hay between the matches of pat. |
split_once | Splits hay at the first match of pat into the parts before and after it. |
replace_first | Returns hay with its first match of pat replaced by with. |
replace_all | Returns hay with every non-overlapping match of pat replaced by with. |
Examples#
use text::{Searchable};
use tests::assert;
assert!("foobar".contains("oba"));
assert!("a,b,,c".split(",").len() == 4);
assert!("a-b-c".replace_all("-", "+") == "a+b+c");