【问题标题】:How do I check to see if a character is a space character in Swift?如何检查一个字符是否是 Swift 中的空格字符?
【发布时间】:2016-05-17 13:03:49
【问题描述】:

在我正在编写的程序中,我需要检查一个字符是否为空格(“”)。目前将此作为条件,但它不起作用。有任何想法吗?提前致谢。

for(var k = indexOfCharBeingExamined; k < lineBeingExaminedChars.count; k++){

    let charBeingExamined = lineBeingExaminedChars[lineBeingExaminedChars.startIndex.advancedBy(k)];

//operations

    if(String(charBeingExamined) == " "){

    //more operations

    }
}

【问题讨论】:

  • 您需要添加 charBeingExamined 的声明方式以及代码的上下文

标签: string swift character spaces


【解决方案1】:

下面的代码是我在 Swift 中使用函数式方法解决这个问题的方法。我做了一个扩展(实际上是两个),但您可以轻松地利用该功能并在其他地方使用它。

extension String {
    
    var isWhitespace: Bool {
        guard !isEmpty else { return true }
        
        let whitespaceChars = NSCharacterSet.whitespacesAndNewlines
        
        return self.unicodeScalars
            .filter { !whitespaceChars.contains($0) }
            .count == 0
    }
    
}

extension Optional where Wrapped == String {
    
    var isNullOrWhitespace: Bool {
        return self?.isWhitespace ?? true
    }
    
}

【讨论】:

  • 使用contains 而不是filter 可能会更好,因为它更便宜。
  • 在 Swift 5.2 中似乎不再需要显式类型。
【解决方案2】:

以下代码对我有用。请注意,使用 'for' 迭代字符串中的字符更容易(下面的第二个示例):

var s = "X yz"
for var i = 0; i < s.characters.count; i++ {
    let x = s[s.startIndex.advancedBy(i)]
    print(x)
    print(String(x) == " ")
}

for c in s.characters {
    print(c)
    print(String(c) == " ")
}

【讨论】:

    【解决方案3】:

    字符串:

    let origin = "Some string with\u{00a0}whitespaces" // \u{00a0} is a no-break space
    

    Oneliner:

    let result = origin.characters.contains { " \u{00a0}".characters.contains($0) }
    

    另一种方法:

    let spaces = NSCharacterSet.whitespaceCharacterSet()
    let result = origin.utf16.contains { spaces.characterIsMember($0) }
    

    输出:

    print(result) // true
    

    不确定你想用空格做什么,因为那样可能会更简单一些。

    【讨论】:

      【解决方案4】:

      只需在您的代码中更改 " " -> "\u{00A0}"

      for(var k = indexOfCharBeingExamined; k < lineBeingExaminedChars.count; k++){
          let charBeingExamined = lineBeingExaminedChars[lineBeingExaminedChars.startIndex.advancedBy(k)];
          if(String(charBeingExamined) == "\u{00A0}"){
              //more operations
          }
      }
      

      【讨论】:

        【解决方案5】:

        仅测试空格:

        func hasWhitespace(_ input: String) -> Bool {
            let inputCharacterSet = CharacterSet(charactersIn: input)
            
            return !inputCharacterSet.intersection(CharacterSet.whitespaces).isEmpty
        }
        

        测试空格和空字符串:

        func hasWhitespace(_ input: String) -> Bool {
            let inputCharacterSet = CharacterSet(charactersIn: input)
            
            return !inputCharacterSet.intersection(CharacterSet.whitespaces).isEmpty || inputCharacterSet.isEmpty
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-23
          • 2011-05-29
          • 2018-11-12
          • 1970-01-01
          • 2014-08-03
          • 2011-03-15
          • 2014-07-24
          相关资源
          最近更新 更多