码迷,mamicode.com
首页 > 编程语言 > 详细

swift - String 子串

时间:2020-03-02 17:34:15      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:int   express   char   ace   wstring   ring   fse   regular   mat   

 

    ///1, 截取规定下标之后的字符串
    func subStringFrom(index: Int) -> String {
        let temporaryString: String = self
        let temporaryIndex = temporaryString.index(temporaryString.startIndex, offsetBy: index)
        return String(temporaryString[temporaryIndex...])
    }

    ///2, 截取规定下标之前的字符串
    func subStringTo(index: Int) -> String {
        let temporaryString = self
        let temporaryIndex = temporaryString.index(temporaryString.startIndex, offsetBy: index)
        return String(temporaryString[...temporaryIndex])
    }
    ///3,替换某个range的字符串
    func replaceStringWithRange(location: Int, length: Int, newString: String) -> String {
        if location + length > self.count {
            return self
        }
        let start = self.startIndex
        let location_start = self.index(start, offsetBy: location)
        let location_end = self.index(location_start, offsetBy: length)
        let result = self.replacingCharacters(in: location_start..<location_end, with: newString)
        return result
    }
    ///4.获取某个range 的子串
    func subStringWithRange(location: Int, length: Int) -> String {
        if location + length > self.count{
            return self
        }
        let str: String = self
        let start = str.startIndex
        let startIndex = str.index(start, offsetBy: location)
        let endIndex = str.index(startIndex, offsetBy: length)
        return String(str[startIndex..<endIndex])
    }
    
    /// 正则匹配第一次出现
    func firstMatchWith(pattern: String) -> NSRange {
        if self.count == 0 {
            return NSMakeRange(0, 0)
        }
        do
        {
            let str: String = self
            let regular = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
            let reg = regular.firstMatch(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSMakeRange(0, str.count))
            if let result = reg {
                return NSMakeRange(result.range.location, result.range.length)
            }
        }catch {
            KimLogError(format: "error: \(error)")
        }
            return NSMakeRange(0, 0)
    }
    
    /// 获取子串的所有range
    static func rangesOfString(_ searchString: String, inString: NSString) -> [NSRange] {
         var results = [NSRange]()
         if searchString.count > 0 && inString.length > 0 {
             var searchRange = NSMakeRange(0, inString.length)
             var range = inString.range(of: searchString, options: [], range: searchRange)
             while (range.location != NSNotFound) {
                 results.append(range)
                 searchRange = NSMakeRange(NSMaxRange(range), inString.length - NSMaxRange(range))
                 range = inString.range(of: searchString, options: [], range: searchRange)
             }
             
         }
         return results
     }

 

swift - String 子串

标签:int   express   char   ace   wstring   ring   fse   regular   mat   

原文地址:https://www.cnblogs.com/daxueshan/p/12396780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!