如何在Swift 4中使用String切片下标?

本文探讨了在Swift 4中如何应对String切片下标的警告,解释了部分范围下标的工作原理,并提供了从Swift 3到Swift 4的代码转换示例,包括使用substring(from:)方法和便利属性。
摘要由CSDN通过智能技术生成

本文翻译自:How can I use String slicing subscripts in Swift 4?

I have the following simple code written in Swift 3: 我有以下用Swift 3编写的简单代码:

let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)

From Xcode 9 beta 5, I get the following warning: 从Xcode 9 beta 5,我收到以下警告:

' substring(to:) ' is deprecated: Please use String slicing subscript with a 'partial range from' operator. 不推荐使用' substring(to:) ':请使用带有'partial range from'运算符的String切片下标。

How can this slicing subscript with partial range from be used in Swift 4? 如何在Swift 4中使用部分范围的切片下标


#1楼

参考:https://stackoom.com/question/35aVE/如何在Swift-中使用String切片下标


#2楼

You should leave one side empty , hence the name "partial range". 您应将一侧留空 ,因此命名为“ partial range”。

let newStr = str[..<index]

The same stands for partial range from operators, just leave the other side empty: 运算符的部分范围相同,只是将另一侧留空:

let newStr = str[index...]

Keep in mind that these range operators return a Substring . 请记住,这些范围运算符返回Substring If you want to convert it to a string, use String 's initialization function: 如果要将其转换为字符串,请使用String的初始化函数:

let newStr = String(str[..<index])

You can read more about the new substrings here . 您可以在此处阅读有关新子字符串的更多信息。


#3楼

Example of uppercasedFirstCharacter convenience property in Swift3 and Swift4. Swift3和Swift4中的uppercasedFirstCharacter便利属性的示例。

Property uppercasedFirstCharacterNew demonstrates how to use String slicing subscript in Swift4. 属性uppercasedFirstCharacterNew演示了如何在Swift4中使用字符串切片下标。

extension String {

   public var uppercasedFirstCharacterOld: String {
      if characters.count > 0 {
         let splitIndex = index(after: startIndex)
         let firstCharacter = substring(to: splitIndex).uppercased()
         let sentence = substring(from: splitIndex)
         return firstCharacter + sentence
      } else {
         return self
      }
   }

   public var uppercasedFirstCharacterNew: String {
      if characters.count > 0 {
         let splitIndex = index(after: startIndex)
         let firstCharacter = self[..<splitIndex].uppercased()
         let sentence = self[splitIndex...]
         return firstCharacter + sentence
      } else {
         return self
      }
   }
}

let lorem = "lorem".uppercasedFirstCharacterOld
print(lorem) // Prints "Lorem"

let ipsum = "ipsum".uppercasedFirstCharacterNew
print(ipsum) // Prints "Ipsum"

#4楼

The conversion of your code to Swift 4 can also be done this way: 您的代码到Swift 4的转换也可以通过以下方式完成:

let str = "Hello, playground"
let index = str.index(of: ",")!
let substr = str.prefix(upTo: index)

You can use the code below to have a new string: 您可以使用下面的代码来创建新字符串:

let newString = String(str.prefix(upTo: index))

#5楼

substring(from: index) Converted to [index...] substring(from:index) 转换为 [index ...]

Check the sample 检查样品

let text = "1234567890"
let index = text.index(text.startIndex, offsetBy: 3)

text.substring(from: index) // "4567890"   [Swift 3]
String(text[index...])      // "4567890"   [Swift 4]

#6楼

Some useful extensions: 一些有用的扩展:

extension String {
    func substring(from: Int, to: Int) -> String {
        let start = index(startIndex, offsetBy: from)
        let end = index(start, offsetBy: to - from)
        return String(self[start ..< end])
    }

    func substring(range: NSRange) -> String {
        return substring(from: range.lowerBound, to: range.upperBound)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值