Swift - Lollipop 1 - Determine if there are any elements in the string

//如下方法: 说白了其实就是判断 string.isEmpty, 如果你的string有可能是null,(null),<null>,则传一个排除为空的参数excludeNull布尔值即可。
    func hasElement(str: String?, excludeNull: Bool = false) -> Bool {
    
    
        if let string = str {
    
    
            if excludeNull {
    
    
                if !string.isEmpty && string.lowercased() != "null" && string.lowercased() != "(null)" && string.lowercased() != "<null>" {
    
    
                    return true
                }
            }else{
    
    
                if !string.isEmpty {
    
    
                    return true
                }
            }
        }
        return false
    }

//排除字符串中的空格
     func trim() -> String {
    
    
        return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
    }
    
//调用
    if hasElement(self.textView.trim()){
    
    
       //即可
    }

What I write or summarize is not difficult, I am not a big cow, nor can I write profound and profound code. It is not easy for me to understand every little knowledge thoroughly. No matter how small the knowledge is, it is useless. It is impossible to learn, so sum up these knowledge points, which can be used in daily work, and perhaps can also help others, record it, very good!

Guess you like

Origin blog.csdn.net/SoftwareDoger/article/details/105864742