[][src]Function phpify::string::explode

pub fn explode<D, S>(
    delimiter: D,
    string: S,
    limit: isize
) -> Option<Vec<String>> where
    D: AsRef<str>,
    S: AsRef<str>, 

Split a string by a string.

Description

Returns an vec of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

Parameters

limit

If limit is set and positive, the returned vec will contain a maximum of limit elements with the last element containing the rest of string.

If the limit parameter is negative, all components except the last -limit are returned.

If the limit parameter is zero, then this is treated as 1.

Examples

Example #1 explode() examples

use phpify::string::explode;

let pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
let pieces = explode(" ", pizza, std::isize::MAX).unwrap();

assert_eq!(pieces, ["piece1", "piece2", "piece3", "piece4", "piece5", "piece6"]);
use phpify::string::explode;

let data = "foo:*:1023:1000::/home/foo:/bin/sh";
let elements = explode(":", data, std::isize::MAX).unwrap();

assert_eq!(elements[0], "foo".to_string());
assert_eq!(elements[1], "*".to_string());

Example #2 explode() return examples

use phpify::string::explode;

let input1 = "hello";
let input2 = "hello,there";
let input3 = ",";

assert_eq!(explode(",", input1, std::isize::MAX).unwrap(), ["hello"]);
assert_eq!(explode(",", input2, std::isize::MAX).unwrap(), ["hello", "there"]);
assert_eq!(explode(",", input3, std::isize::MAX).unwrap(), ["", ""]);

Example #3 limit parameter examples

use phpify::string::explode;

let str = "one|two|three|four";

assert_eq!(explode("|", str, 2).unwrap(), ["one", "two|three|four"]);
assert_eq!(explode("|", str, -1).unwrap(), ["one", "two", "three"]);