Swift: How to get substring from start to last index of character

前端 未结 22 870
感情败类
感情败类 2020-11-30 19:09

I want to learn the best/simplest way to turn a string into another string but with only a subset, starting at the beginning and going to the last index of a character.

22条回答
  •  广开言路
    2020-11-30 20:00

    Swift 2.0 The code below is tested on XCode 7.2 . Please refer to the attached screenshot at the bottom

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            var mainText = "http://stackoverflow.com"
    
            var range = Range(start: mainText.startIndex.advancedBy(7), end: mainText.startIndex.advancedBy(24))
            var subText = mainText.substringWithRange(range)
    
    
            //OR Else use below for LAST INDEX
    
            range = Range(start: mainText.startIndex.advancedBy(7), end: mainText.endIndex)
            subText = mainText.substringWithRange(range)
        }
    }
    

提交回复
热议问题