最近写项目用到随机字符串,然后从随机串中截取一段,查阅了资料后总结如下;
随机生成一个120个字符组成的字符串:
var randomStr = ""
println("start:")
for i in 1 ... 120{
var num = 48 + arc4random()%74 //自定义ASCII码范围从48到122
var randomCharacter = Character( UnicodeScalar(num))
randomStr.append(randomCharacter)
}
截取中间的一段字符串:从第6个到第46个
let rangeOfSub = Range(start: advance(randomStr.startIndex, 5),
end: advance(randomStr.startIndex, 45))
var randomSub = randomStr.substringWithRange(rangeOfKey)