• swift


        ///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
         }
  • 相关阅读:
    使用Word发布文章到 WordPress 博客
    Wordpress上传到阿里云服务器
    IntelliJ设置鼠标悬浮提示和修改快捷键
    梅塔幻灯片如何设置图片高度不被裁减
    更改XAMPP中MySQL数据库的端口号
    PHP开启cURL功能
    Android Studio使用百度地图示例BaiduMapsApiASDemo
    CocosCreator反射在Android中的使用
    Android Studio新建一个HelloWorld 程序(App)
    无法中止进程无法访问操作拒绝访问
  • 原文地址:https://www.cnblogs.com/daxueshan/p/12396780.html
Copyright © 2020-2023  润新知