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

前端 未结 22 844
感情败类
感情败类 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 19:46

    You can use these extensions:

    Swift 2.3

     extension String
        {
            func substringFromIndex(index: Int) -> String
            {
                if (index < 0 || index > self.characters.count)
                {
                    print("index \(index) out of bounds")
                    return ""
                }
                return self.substringFromIndex(self.startIndex.advancedBy(index))
            }
    
            func substringToIndex(index: Int) -> String
            {
                if (index < 0 || index > self.characters.count)
                {
                    print("index \(index) out of bounds")
                    return ""
                }
                return self.substringToIndex(self.startIndex.advancedBy(index))
            }
    
            func substringWithRange(start: Int, end: Int) -> String
            {
                if (start < 0 || start > self.characters.count)
                {
                    print("start index \(start) out of bounds")
                    return ""
                }
                else if end < 0 || end > self.characters.count
                {
                    print("end index \(end) out of bounds")
                    return ""
                }
                let range = Range(start: self.startIndex.advancedBy(start), end: self.startIndex.advancedBy(end))
                return self.substringWithRange(range)
            }
    
            func substringWithRange(start: Int, location: Int) -> String
            {
                if (start < 0 || start > self.characters.count)
                {
                    print("start index \(start) out of bounds")
                    return ""
                }
                else if location < 0 || start + location > self.characters.count
                {
                    print("end index \(start + location) out of bounds")
                    return ""
                }
                let range = Range(start: self.startIndex.advancedBy(start), end: self.startIndex.advancedBy(start + location))
                return self.substringWithRange(range)
            }
        }
    

    Swift 3

    extension String
    {   
        func substring(from index: Int) -> String
        {
            if (index < 0 || index > self.characters.count)
            {
                print("index \(index) out of bounds")
                return ""
            }
            return self.substring(from: self.characters.index(self.startIndex, offsetBy: index))
        }
    
        func substring(to index: Int) -> String
        {
            if (index < 0 || index > self.characters.count)
            {
                print("index \(index) out of bounds")
                return ""
            }
            return self.substring(to: self.characters.index(self.startIndex, offsetBy: index))
        }
    
        func substring(start: Int, end: Int) -> String
        {
            if (start < 0 || start > self.characters.count)
            {
                print("start index \(start) out of bounds")
                return ""
            }
            else if end < 0 || end > self.characters.count
            {
                print("end index \(end) out of bounds")
                return ""
            }
            let startIndex = self.characters.index(self.startIndex, offsetBy: start)
            let endIndex = self.characters.index(self.startIndex, offsetBy: end)
            let range = startIndex.. String
        {
            if (start < 0 || start > self.characters.count)
            {
                print("start index \(start) out of bounds")
                return ""
            }
            else if location < 0 || start + location > self.characters.count
            {
                print("end index \(start + location) out of bounds")
                return ""
            }
            let startIndex = self.characters.index(self.startIndex, offsetBy: start)
            let endIndex = self.characters.index(self.startIndex, offsetBy: start + location)
            let range = startIndex..

    Usage:

    let string = "www.stackoverflow.com"        
    let substring = string.substringToIndex(string.characters.count-4)
    

提交回复
热议问题