返回

Swift 扩展 String:一个全能字符串工具箱

IOS

Swift 扩展 String:一个全能字符串工具箱

在 Swift 中,String 类型是文本处理的基础,经常需要对字符串进行各种操作。为了简化这些操作,我们可以使用 String 的扩展来实现。本文提供了 Swift 中 String 类型最常用的扩展,包括去掉首尾空格、去掉首尾空格与换行、去掉所有空格、获取字符串在 label 中显示的宽、获取字符串在 label 中显示的高、生成随机字符串、验证、邮箱验证、电话验证、是否包含汉字以及时间相关,帮助您轻松处理字符串。

常用功能

  • 去掉首尾空格 :使用 trimmingCharacters(in: .whitespacesAndNewlines) 方法可以去掉字符串的首尾空格。
let str = "  Hello World  "
let trimmedStr = str.trimmingCharacters(in: .whitespacesAndNewlines)
print(trimmedStr) // "Hello World"
  • 去掉首尾空格与换行 :使用 trimmingCharacters(in: .whitespacesAndNewlines) 方法可以去掉字符串的首尾空格与换行。
let str = "  Hello\nWorld  "
let trimmedStr = str.trimmingCharacters(in: .whitespacesAndNewlines)
print(trimmedStr) // "Hello\nWorld"
  • 去掉所有空格 :使用 components(separatedBy: " ").joined() 方法可以去掉字符串中的所有空格。
let str = "Hello World"
let trimmedStr = str.components(separatedBy: " ").joined()
print(trimmedStr) // "HelloWorld"
  • 获取字符串在 label 中显示的宽 :使用 size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17)]) 方法可以获取字符串在 label 中显示的宽。
let str = "Hello World"
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 17)
label.text = str
let width = label.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width
print(width) // 100.0
  • 获取字符串在 label 中显示的高 :使用 size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17)]) 方法可以获取字符串在 label 中显示的高。
let str = "Hello World"
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 17)
label.text = str
let height = label.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).height
print(height) // 20.0
  • 生成随机字符串 :使用 UUID().uuidString 方法可以生成随机字符串。
let randomStr = UUID().uuidString
print(randomStr) // "83A620E9-4033-4317-99E4-9C7A5A3EB432"
  • 验证 :使用 isEmpty 方法可以验证字符串是否为空。
let str = "Hello World"
let isEmpty = str.isEmpty
print(isEmpty) // false
  • 邮箱验证 :使用 isValidEmail 方法可以验证邮箱是否有效。
let email = "example@gmail.com"
let isValidEmail = email.isValidEmail
print(isValidEmail) // true
  • 电话验证 :使用 isValidPhoneNumber 方法可以验证电话号码是否有效。
let phone = "1234567890"
let isValidPhoneNumber = phone.isValidPhoneNumber
print(isValidPhoneNumber) // true
  • 是否包含汉字 :使用 containsChineseCharacters 方法可以验证字符串是否包含汉字。
let str = "你好,世界"
let containsChineseCharacters = str.containsChineseCharacters
print(containsChineseCharacters) // true

时间相关

  • 获取当前时间 :使用 Date() 方法可以获取当前时间。
let now = Date()
print(now) // 2023-03-08 10:26:09 +0800
  • 获取当前时间戳 :使用 timeIntervalSince1970 方法可以获取当前时间戳。
let now = Date()
let timestamp = now.timeIntervalSince1970
print(timestamp) // 1678271569.123456
  • 将时间戳转换为 Date 对象 :使用 Date(timeIntervalSince1970:) 方法可以将时间戳转换为 Date 对象。
let timestamp = 1678271569.123456
let date = Date(timeIntervalSince1970: timestamp)
print(date) // 2023-03-08 10:26:09 +0800

结语

本文提供了 Swift 中 String 类型最常用的扩展,包括去掉首尾空格、去掉首尾空格与换行、去掉所有空格、获取字符串在 label 中显示的宽、获取字符串在 label 中显示的高、生成随机字符串、验证、邮箱验证、电话验证、是否包含汉字以及时间相关,帮助您轻松处理字符串。希望这些扩展能够在您的项目中发挥作用,让您的代码更加简洁高效。