拆分字母串入三分球迅速 [英] Splitting a string of letters into 3s in swift

查看:124
本文介绍了拆分字母串入三分球迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里斯威夫特新手。我想我的一些蟒蛇code的转换迅速和IM停留在的地步,我需要的字母串分割成和阵列,每个项目为3个字母:

Swift newbie here. I am trying to convert some of my python code to swift and im stuck at the point where I need to split a string of letters into and array with each item being 3 letters:

例如我的Python code是如下:

For example my python code is as follows:

name = "ATAGASSTSSGASTA"
threes =[]
for start in range(0, len(name),3):
   threes.append(name[start : start + 3])
print threes

有关香港专业教育学院迅速来到这么远:

For swift ive come as far as this:

var name = "ATAGASSTSSGASTA"
let namearr = Array(name)
let threes = []
threes.append(namearr[0...3])

这给了我一个错误。

我知道有可能做到这一点的更简单的方法,但我一直没能找到我的研究什么。任何帮助AP preciated!

I realize there may be an much easier way to do this, but I have not been able to find anything in my research. Any help is appreciated!

推荐答案

这是简单而SWIFTY办法做到这一点是映射使用步幅和字符数组事先功能:

An easy and Swifty way to do this is to map an array of chars using the stride and advance functions:

let name = Array("ATAGASSTSSGASTA")

let splitName = map(stride(from: 0, to: name.count, by: 3)) {
    String(name[$0..<advance($0, 3, name.count)])
}

这篇关于拆分字母串入三分球迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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