修剪字符不适用于 iOS 10.3 Xcode8.3 [英] trimmingCharacters not work on iOS 10.3 Xcode8.3

查看:33
本文介绍了修剪字符不适用于 iOS 10.3 Xcode8.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,我使用 Xcode 8.3(swift 3.1),trimmingCharacters 功能不起作用.我的代码如下:

Please help me, I use Xcode 8.3(swift 3.1), the function trimmingCharacters not work. My code as below:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if var searchStr = textField.text{
       let _searchStr = searchStr.trimmingCharacters(in: .whitespaces)
       print("After trimming:\(_searchStr)")
   }
}

textfield 中的输入是 409 huỳnh 打印结果是 409 huỳnh 不符合预期:409huỳnh.

The input in textfield is 409 huỳnh and the print result is 409 huỳnh not as expected: 409huỳnh .

推荐答案

来自文档:

一个新的字符串,通过从两端去除包含在集合中的接收者字符组成.

A new string made by removing from both ends of the receiver characters contained in set.

不会删除字符串中的字符.

It does not remove characters within the string.

您可以用正则表达式替换字符串中对应于 .whitespaces 字符集的空格:

You can replace whitespaces – corresponding to the .whitespaces character set – in the string with regular expression:

let _searchStr = searchStr.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)

这篇关于修剪字符不适用于 iOS 10.3 Xcode8.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆