If I want to get a value from the NSString "hello World"
, what should I use?
The return value I want is "hello World"
.
The smileys can be any string. so i need some regexp to this.
If I want to get a value from the NSString "hello World"
, what should I use?
The return value I want is "hello World"
.
The smileys can be any string. so i need some regexp to this.
There's more than one way to do it.
First: String in Swift 1.2 is bridged to NSString properly so the answer could be : How to get substring of NSString?
Second: This answer will deal with emoticon characters too.
var s = "hello World"
let index = advance(s.startIndex, 1) // index is a String.Index to the 2nd glyph, “h”
let endIndex = advance(s.startIndex, 12)
let substring=s[index..<endIndex] // here you got "hello World"
if let emojiRange = s[index...s.endIndex].rangeOfString("") {
let substring2 = s[index..<emojiRange.startIndex] // here you get "hello World"
}