将字符串拆分为偏移子字符串的功能方法 [英] Functional way to split string into offset substrings

查看:34
本文介绍了将字符串拆分为偏移子字符串的功能方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 rustc 1.0.0-beta (9854143cb 2015-04-02) (built 2015-04-02)

我的目标是将长度为 n 的字符串拆分为 n-k+1 个长度为 k 的偏移子串.我的意思是如果你有一个字符串:

My goal is to split a string of length n into n-k+1 offset substrings of length k. What I mean by that is if you have a string:

ABCDEF

我正在尝试获取包含任意长度 k 的偏移子字符串的向量/迭代器.例如,k=3 将产生

I'm trying to obtain a vector/iterator that contains offset substrings of arbitrary length k. For example, k=3 would yield

ABC
 BCD
  CDE
   DEF

并且 k=2 将产生:

AB
 BC
  CD
   DE
    EF

请注意,上面包含的空格仅用于对齐子字符串以显示它们之间的关系.输出向量将只包括 ABBCCD 等.此外,只支持 ASCII 是可以的,尽管我更喜欢更安全的更通用的解决方案.

Note that the whitespace is only included above to align the substrings to show how they are related. The output vector would only include AB,BC, CD, etc. Also, it's ok to only support ASCII, although I would prefer a safer more generic solution.

尽管看起来很痛苦,但以下过程代码似乎有效:

As painful as it is to look at, the following procedural code seems to work:

fn offset_slices(s: &str, n: usize) -> Vec<&str> {
    let mut slices: Vec<&str> = Vec::new();
    for (i,_) in s.chars().enumerate() {
        if i > s.len() - n {
            break;
        }
        slices.push(&s[i..(i+n)]);
    }
    slices
}

但这很糟糕,我更喜欢功能更强大的解决方案.我花了几个小时试图找出一种方法,并在此过程中学到了很多东西,但我对这个方法很困惑.

But it's nasty and I would prefer a more functional solution. I spent a couple hours trying to figure out a way, and learned a lot in the process, but I'm stumped on this one.

有什么想法吗?

PS - 我真的很惊讶上面的 slices.push(&s[i..(i+n)]) 甚至可以编译.它只是返回指向输入的各个位置的指针吗?

PS - I'm really surprised that the slices.push(&s[i..(i+n)]) above even compiles. Is it just returning pointers to various locations of the input?

推荐答案

fn offset_slices(s: &str, n: usize) -> Vec<&str> {
    (0 .. s.len() - n + 1).map(|i| &s[i .. i + n]).collect()
}

这篇关于将字符串拆分为偏移子字符串的功能方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆