NSString+CNLiveExtensions.swift 4.53 KB
//
//  NSString+CNLiveExtensions.swift
//  metaCode
//
//  Created by open on 2023/6/6.
//

import Foundation
import UIKit

extension NSString {
    
    /// 判断是不是纯数字
    /// - Parameter string: 字符串
    /// - Returns: 结果
    @discardableResult
    func isPurnInt() -> Bool {
        let scan: Scanner = Scanner(string: self as String)
        var val:Int = 0
        return scan.scanInt(&val) && scan.isAtEnd
    }
    
    
    /// 当前字符串是否为空
    /// - Returns: YES:不为空, NO为空
    func isNotBlank()->Bool {
       
        let blank:NSCharacterSet = NSCharacterSet.whitespacesAndNewlines as NSCharacterSet

        let count = self.length
        for i in (0..<count) {
            let c: unichar = self.character(at: i)
            if(!blank.characterIsMember(c)){
                return true
            }
        }
        return false;
    }
    
    /// 字符串转秒数
    /// - Parameter dateFormat: 时间格式
    /// - Returns: 返回
    func timeStrChangeTotimeInterval(_ dateFormat:String? = "yyyy-MM-dd HH:mm:ss") -> String {
        if !self.isNotBlank() {
            return ""
        }
        let format = DateFormatter.init()
        format.dateStyle = .medium
        format.timeStyle = .short
        format.locale = Locale(identifier: "zh_CN")
        format.timeZone = TimeZone(abbreviation: "UTC+8:00")
        if dateFormat == nil {
            format.dateFormat = "yyyy-MM-dd HH:mm:ss"
        }else{
            format.dateFormat = dateFormat
        }
        let date = format.date(from: self as String)
        return "\(Int(date!.timeIntervalSince1970))"
    }
    
    /// 字符串转date
    /// - Parameter dateFormat: 日期格式
    /// - Returns: 返回
    func timeStrChangeTotimedate(_ dateFormat:String? = "yyyy-MM-dd HH:mm:ss") -> NSDate {
        if !self.isNotBlank() {
            return NSDate()
        }
        let format = DateFormatter.init()
        format.dateStyle = .medium
        format.timeStyle = .short
        format.locale = Locale(identifier: "zh_CN")
        format.timeZone = TimeZone(abbreviation: "UTC+8:00")
        if dateFormat == nil {
            format.dateFormat = "yyyy-MM-dd HH:mm:ss"
        }else{
            format.dateFormat = dateFormat
        }
        return format.date(from: self as String)! as NSDate
    }
    
    /// 保留2位小数
    /// - Returns: 返回
    func keepTwoDecimalPlaces() -> String {
        let mSelf = NSMutableString.init(string: self)
        if self.contains(".") {
            let decimalLocation = mSelf.range(of: ".").location
            var lastDecimalLocationLength = mSelf.length - decimalLocation - 1
            if lastDecimalLocationLength > 2 {
                while lastDecimalLocationLength != 2 {
                    mSelf.deleteCharacters(in: NSMakeRange(mSelf.length - 1, 1))
                    lastDecimalLocationLength-=1
                }
            } else if (lastDecimalLocationLength < 2) {
                while lastDecimalLocationLength != 0 {
                    mSelf.append("0")
                    lastDecimalLocationLength-=1
                }
            }
        } else {
            mSelf.append(".00")
        }
        return mSelf.copy() as! String
    }
    
    ///  查看允不允许输入 例如 允许输入1、2时传入12
    /// - Parameter string: 输入字符串
    /// - Returns: 返回
    func allowWriteString(string: String) -> Bool {
        let cs = NSCharacterSet.init(charactersIn: string)
        let filtered_array = components(separatedBy: cs as CharacterSet) as NSArray
        let filtered = filtered_array.componentsJoined(by: "")
        return self as String == filtered
    }
    
    /**
     保留2位小数
     */
    func keepTwoDecimalPlaces() -> NSString{
        var mSelf = NSMutableString(string: self)
        if self.contains("."){
            let decimalLocation = mSelf.range(of: ".").location
            var lastDecimalLocationLength = mSelf.length - decimalLocation - 1
            if lastDecimalLocationLength > 2{
                while lastDecimalLocationLength != 2 {
                    mSelf.deleteCharacters(in: NSRange(location: mSelf.length-1, length: 1))
                    lastDecimalLocationLength -= lastDecimalLocationLength
                }
            }else if (lastDecimalLocationLength < 2) {
                while (lastDecimalLocationLength != 0) {
                    mSelf.append("0")
                    lastDecimalLocationLength -= 1
                }
            }
        }else {
            mSelf.append(".00")
        }
        return mSelf
    }
}