Check empty string in Swift?

前端 未结 15 1943
臣服心动
臣服心动 2020-11-28 03:51

In Objective C, one could do the following to check for strings:

if ([myString isEqualToString:@\"\"]) {
    NSLog(@\"m         


        
15条回答
  •  一生所求
    2020-11-28 04:37

    Here is how I check if string is blank. By 'blank' I mean a string that is either empty or contains only space/newline characters.

    struct MyString {
      static func blank(text: String) -> Bool {
        let trimmed = text.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        return trimmed.isEmpty
      }
    }
    

    How to use:

    MyString.blank(" ") // true
    

提交回复
热议问题