How do I split a string in Rust?

前端 未结 5 1101
情深已故
情深已故 2020-11-30 19:18

From the documentation, it\'s not clear. In Java you could use the split method like so:

\"some string          


        
相关标签:
5条回答
  • 2020-11-30 20:07

    Use split()

    let mut split = "some string 123 ffd".split("123");
    

    This gives an iterator, which you can loop over, or collect() into a vector.

    for s in split {
        println!("{}", s)
    }
    let vec = split.collect::<Vec<&str>>();
    // OR
    let vec: Vec<&str> = split.collect();
    
    0 讨论(0)
  • 2020-11-30 20:10

    There is a special method split for struct String:

    fn split<'a, P>(&'a self, pat: P) -> Split<'a, P> where P: Pattern<'a>
    

    Split by char:

    let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
    assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
    

    Split by string:

    let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
    assert_eq!(v, ["lion", "tiger", "leopard"]);
    

    Split by closure:

    let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).collect();
    assert_eq!(v, ["abc", "def", "ghi"]);
    
    0 讨论(0)
  • 2020-11-30 20:15

    There's also split_whitespace()

    fn main() {
        let words: Vec<&str> = "   foo   bar\t\nbaz   ".split_whitespace().collect();
        println!("{:?}", words);
        // ["foo", "bar", "baz"] 
    }
    
    0 讨论(0)
  • 2020-11-30 20:18

    There are three simple ways:

    1. By separator:

      s.split("separator")  |  s.split('/')  |  s.split(char::is_numeric)
      
    2. By whitespace:

      s.split_whitespace()
      
    3. By newlines:

      s.lines()
      

    The result of each kind is an iterator:

    let text = "foo\r\nbar\n\nbaz\n";
    let mut lines = text.lines();
    
    assert_eq!(Some("foo"), lines.next());
    assert_eq!(Some("bar"), lines.next());
    assert_eq!(Some(""), lines.next());
    assert_eq!(Some("baz"), lines.next());
    
    assert_eq!(None, lines.next());
    
    0 讨论(0)
  • 2020-11-30 20:21

    split returns an Iterator, which you can convert into a Vec using collect: split_line.collect::<Vec<_>>(). Going through an iterator instead of returning a Vec directly has several advantages:

    • split is lazy. This means that it won't really split the line until you need it. That way it won't waste time splitting the whole string if you only need the first few values: split_line.take(2).collect::<Vec<_>>(), or even if you need only the first value that can be converted to an integer: split_line.filter_map(|x| x.parse::<i32>().ok()).next(). This last example won't waste time attempting to process the "23.0" but will stop processing immediately once it finds the "1".
    • split makes no assumption on the way you want to store the result. You can use a Vec, but you can also use anything that implements FromIterator<&str>, for example a LinkedList or a VecDeque, or any custom type that implements FromIterator<&str>.
    0 讨论(0)
提交回复
热议问题