[][src]Function tao_of_rust::ch08::strings::strings_search

pub fn strings_search()

字符串查找

Base usage: 存在性判断

fn main() {
    let bananas = "bananas";
    assert!(bananas.contains('a'));
    assert!(bananas.contains("an"));
    assert!(bananas.contains(char::is_lowercase));
    assert!(bananas.starts_with('b'));
    assert!(!bananas.ends_with("nana"));
}Run

Base usage: find方法查找位置

fn main() {
    let s = "Löwe 老虎 Léopard";
    assert_eq!(s.find('w'), Some(3));
    assert_eq!(s.find('老'), Some(6));
    assert_eq!(s.find('虎'), Some(9));
    assert_eq!(s.find("é"), Some(14));
    assert_eq!(s.find("Léopard"), Some(13));
    assert_eq!(s.rfind('L'), Some(13));
    assert_eq!(s.find(char::is_whitespace), Some(5));
    assert_eq!(s.find(char::is_lowercase), Some(1));
}Run

Base usage: split系列方法分割字符串

fn main() {
    let s = "Löwe 虎 Léopard";
    let v  = s.split( |c|
        (c as u32) >= (0x4E00 as u32) &&  (c as u32) <= (0x9FA5 as u32)
    ).collect::<Vec<&str>>();
    assert_eq!(v, ["Löwe ", " Léopard"]);
    let v = "abc1defXghi".split(|c|
        c == '1' || c == 'X'
    ).collect::<Vec<&str>>();;
   assert_eq!(v, ["abc", "def", "ghi"]);
   let v = "Mary had a little lambda"
       .splitn(3, ' ')
       .collect::<Vec<&str>>();;
   assert_eq!(v, ["Mary", "had", "a little lambda"]);
   let v = "A.B.".split(".").collect::<Vec<&str>>();;
   assert_eq!(v, ["A", "B", ""]);
   let v = "A.B.".split_terminator('.').collect::<Vec<&str>>();;
   assert_eq!(v, ["A", "B"]);
   let v = "A..B..".split(".").collect::<Vec<&str>>();;
   assert_eq!(v, ["A", "", "B", "", ""]);
   let v = "A..B..".split_terminator(".").collect::<Vec<&str>>();;
   assert_eq!(v, ["A", "", "B", ""]);
}Run

Base usage: matches系列方法匹配并捕获字符串

fn main() {
    let v = "abcXXXabcYYYabc"
        .matches("abc").collect::<Vec<&str>>();
    assert_eq!(v, ["abc", "abc", "abc"]);
    let v = "1abc2abc3"
        .rmatches(char::is_numeric).collect::<Vec<&str>>();
    assert_eq!(v, ["3", "2", "1"]);
    let v = "abcXXXabcYYYabc"
       .match_indices("abc").collect::<Vec<_>>();
   assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
   let v = "abcXXXabcYYYabc"
       .rmatch_indices("abc").collect::<Vec<_>>();
   assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
}Run

Base usage: trim系列方法删除字符串空格、制表符、换行符

fn main() {
    let s = " Hello\tworld\t";
    assert_eq!("Hello\tworld", s.trim());
    assert_eq!("Hello\tworld\t", s.trim_left());
    assert_eq!(" Hello\tworld", s.trim_right());
}Run

Base usage: trim_matches系列方法自定义删除规则

fn main() {
    assert_eq!("Hello\tworld\t".trim_matches('\t'), "Helloworld");
    assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
    assert_eq!("123foo1bar123"
        .trim_matches(char::is_numeric), "foo1bar");
    let x: &[char] = &['1', '2'];
    assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
    assert_eq!(
        "1foo1barXX".trim_matches(|c| c == '1' || c == 'X'),
       "foo1bar"
    );
   assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
   assert_eq!(
       "123foo1bar123".trim_left_matches(char::is_numeric),
       "foo1bar123");
   let x: &[char] = &['1', '2'];
   assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
   assert_eq!(
       "1fooX".trim_right_matches(|c| c == '1' || c == 'X'),
       "1foo"
   );
}Run

Base usage: replace替换匹配

fn main() {
    let s = "Hello\tworld\t";
    assert_eq!("Hello world ", s.replace("\t", " "));
    assert_eq!("Hello world", s.replace("\t", " ").trim());
    let s = "this is old old 123";
    assert_eq!("this is new new 123", s.replace("old", "new"));
    assert_eq!("this is new old 123", s.replacen("old", "new", 1));
    assert_eq!("this is ald ald 123", s.replacen('o', "a", 3));
    assert_eq!(
        "this is old old new23",
       s.replacen(char::is_numeric, "new", 1)
   );
}Run