-2

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.

Csabi
  • 3,097
  • 17
  • 59
  • 107
  • Hello, I think is not question about range but about getting substring of string – Csabi Aug 25 '15 at 11:40
  • 4
    I've voting to close this question as unclear. There are no details provided on how to get from A to B. Is the difference that we want the characters in the middle? Do we want to just eliminate any emoji no matter where it is? Do we want to simply trim the first character of each end no matter what it is? Do we want to trim characters off the ends but only if there are emojis? There's no clear description of what is *actually* trying to be accomplished. If the question is literally 'How do I return `"hello World"`?', then the answer is simple: `return "hello World"`. – nhgrif Aug 25 '15 at 12:26
  • http://stackoverflow.com/questions/31679148/swift-function-to-parse-string-from-to – SwiftStudier Aug 25 '15 at 14:42

1 Answers1

0

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"
}
Community
  • 1
  • 1
Csabi
  • 3,097
  • 17
  • 59
  • 107