[英]String: substringToIndex for unicode scalars
To get the first 50 characters of a String
, I can do 要获取String
的前50个字符,我可以
s.substringToIndex(s.startIndex.advancedBy(50)))
How can I get the first 50 unicode scalars of a String
? 如何获得String
的前50个unicode标量?
I can get a String.UnicodeScalarView
object using s.unicodeScalars
but I don't know how to go from there. 我可以使用s.unicodeScalars
得到一个String.UnicodeScalarView
对象,但是我不知道该怎么去。
s.unicodeScalars
returns a String.UnicodeScalarView
which conforms to CollectionType
. s.unicodeScalars
返回符合CollectionType
的String.UnicodeScalarView
。 You can get a subsequence of the collection with 您可以通过以下方式获得集合的子序列
u = s.unicodeScalars
u[u.startIndex ..< u.startIndex.advancedBy(50)]
or, in the case of an initial sequence, more simply with 或者,如果是初始序列,则更简单地使用
u.prefix(50)
There is a small difference: Subscripting requires that the specified subrange exists, and will terminate with a runtime exception otherwise. 差别很小:下标要求指定的子范围存在,否则将以运行时异常终止。 prefix()
truncates at the given index, ie it returns a subsequence with 50 or less elements. prefix()
在给定索引处截断 ,即返回具有50个或更少元素的子序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.