NSString's substringWithRange: should work fine.
That depends on what you mean by fine. Yes, it'll work fine if the string only contains characters encodable as ASCII. For anything more complex than that, even for something as simple as Latin-1,
-substringWithRange:
makes it easy to fall into some common pitfalls. The most common of these are:
you might erroneously split a surrogate pair
you might accidentally split a character from its combining accents
you might erroneously assume that, for two strings to be equal, they must have the same length
The Swift string API is designed to deal with the complexities of non-ASCII string processing. The cost of that design is that you lose the ability to treat strings like ASCII if you know they really are ASCII (for example, if you're dealing with strings in a network protocol).
Which brings us back to the original question. The example only makes sense if you can guarantee that
s
is an ASCII string. So, @Shuhao, are you asking about how to deal nicely with ASCII strings? Or about dealing with strings that are user visible?
IMPORTANT I've used the term ASCII deliberately here because some of the assumptions I've described above don't hold for English. Assuming that ASCII is sufficient to represent English would be naïve. For example:
import Foundation
let s1 = "na\u{ef}ve"
let s2 = "nai\u{0308}ve"
println(s1) // prints "naïve"
println(s2) // prints "naïve"
println(s1 == s2) // prints "true"
println(s1.dataUsingEncoding(NSUTF8StringEncoding) == s2.dataUsingEncoding(NSUTF8StringEncoding)) // prints "false"
Share and Enjoy
—
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"