Move History

Rooted by: String Split
Fork Selected
  • Code
    fn split(string: &str, separator: char) -> Vec<&str> {
        string.split(separator).collect()
    }
    
    Test Cases
    #[test]
    fn test() {
        assert_eq!(split("Hello World", ' '), ["Hello", "World"]);
        assert_eq!(split("John-brings-his-cat", '-'), ["John", "brings", "his", "cat"]);
    }
    
  • Code
    • #include <string>
    • #include <sstream>
    • #include <vector>
    • auto split(const std::string& str, char sep) {
    • auto result = std::vector<std::string>{};
    • auto stream = std::stringstream(str);
    • auto buffer = std::string{};
    • while (std::getline(stream, buffer, sep)) result.emplace_back(buffer);
    • return result;
    • fn split(string: &str, separator: char) -> Vec<&str> {
    • string.split(separator).collect()
    • }
    Test Cases
    • // TODO: Replace examples and use TDD by writing your own tests
    • Describe(any_group_name_you_want)
    • {
    • It(should_do_something)
    • {
    • Assert::That(split("Hello World", ' '), Equals(std::vector<std::string>{"Hello", "World"}));
    • Assert::That(split("John-brings-his-cat", '-'), Equals(std::vector<std::string>{"John","brings","his","cat"}));
    • }
    • };
    • #[test]
    • fn test() {
    • assert_eq!(split("Hello World", ' '), ["Hello", "World"]);
    • assert_eq!(split("John-brings-his-cat", '-'), ["John", "brings", "his", "cat"]);
    • }