如何将字符串拆分为等长的子字符串? [英] How to split string into equal-length substrings?
本文介绍了如何将字符串拆分为等长的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Scala 中寻找优雅的 方式来分割给定的字符串转换为固定大小的子字符串(序列中的最后一个字符串可能更短)。
Im looking for an elegant way in Scala to split a given string into substrings of fixed size (the last string in the sequence might be shorter).
所以
So
split("Thequickbrownfoxjumps", 4)
会产生
["Theq","uick","brow","nfox","jump","s"]
当然,我可以简单地使用循环,但必须有一个更优雅的(功能样式)解决方案。
Of course I could simply use a loop but there has to be a more elegant (functional style) solution.
推荐答案
scala> val grouped = "Thequickbrownfoxjumps".grouped(4).toList
grouped: List[String] = List(Theq, uick, brow, nfox, jump, s)
这篇关于如何将字符串拆分为等长的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文