在 Swift 中从字符串中删除第一个字符的最简洁方法是什么? [英] What is the most succinct way to remove the first character from a string in Swift?

查看:39
本文介绍了在 Swift 中从字符串中删除第一个字符的最简洁方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从字符串中删除第一个字符.到目前为止,我想出的最简洁的事情是:

I want to delete the first character from a string. So far, the most succinct thing I've come up with is:

display.text = display.text!.substringFromIndex(advance(display.text!.startIndex, 1))

我知道由于 Unicode,我们不能用 Int 索引字符串,但是这个解决方案看起来非常冗长.有没有我可以忽略的另一种方式?

I know we can't index into a string with an Int because of Unicode, but this solution seems awfully verbose. Is there another way that I'm overlooking?

推荐答案

如果您使用的是 Swift 3,则可以忽略此答案的第二部分.好消息是,这现在又变得简洁了!只需使用 String 的新 remove(at:) 方法.

If you're using Swift 3, you can ignore the second section of this answer. Good news is, this is now actually succinct again! Just using String's new remove(at:) method.

var myString = "Hello, World"
myString.remove(at: myString.startIndex)

myString // "ello, World"

<小时>

我喜欢全局 dropFirst() 函数.

let original = "Hello" // Hello
let sliced = dropFirst(original) // ello

它简短、清晰,适用于任何符合 Sliceable 协议的内容.

It's short, clear, and works for anything that conforms to the Sliceable protocol.

如果您使用的是 Swift 2,则此答案已更改.您仍然可以使用 dropFirst,但不能不从字符串 characters 属性中删除第一个字符,然后将结果转换回字符串.dropFirst 也变成了一个方法,而不是一个函数.

If you're using Swift 2, this answer has changed. You can still use dropFirst, but not without dropping the first character from your strings characters property and then converting the result back to a String. dropFirst has also become a method, not a function.

let original = "Hello" // Hello
let sliced = String(original.characters.dropFirst()) // ello

另一种方法是使用后缀函数拼接字符串的UTF16View.当然,这也必须在之后转换回字符串.

Another alternative is to use the suffix function to splice the string's UTF16View. Of course, this has to be converted back to a String afterwards as well.

let original = "Hello" // Hello
let sliced = String(suffix(original.utf16, original.utf16.count - 1)) // ello

所有这些都是说,我最初提供的解决方案在较新版本的 Swift 中并不是最简洁的方法.如果您正在寻找一个简短而直观的解决方案,我建议您使用 removeAtIndex() 回到@chris 的解决方案.

All this is to say that the solution I originally provided has turned out not to be the most succinct way of doing this in newer versions of Swift. I recommend falling back on @chris' solution using removeAtIndex() if you're looking for a short and intuitive solution.

var original = "Hello" // Hello
let removedChar = original.removeAtIndex(original.startIndex)

original // ello

正如@vacawama 在下面的评论中指出的那样,另一个不修改原始字符串的选项是使用 substringFromIndex.

And as pointed out by @vacawama in the comments below, another option that doesn't modify the original String is to use substringFromIndex.

let original = "Hello" // Hello
let substring = original.substringFromIndex(advance(original.startIndex, 1)) // ello

或者,如果您碰巧想从字符串的开头和结尾删除一个字符,您可以使用 substringWithRange.当 startIndex + n >endIndex - m.

Or if you happen to be looking to drop a character off the beginning and end of the String, you can use substringWithRange. Just be sure to guard against the condition when startIndex + n > endIndex - m.

let original = "Hello" // Hello

let newStartIndex = advance(original.startIndex, 1)
let newEndIndex = advance(original.endIndex, -1)

let substring = original.substringWithRange(newStartIndex..<newEndIndex) // ell

最后一行也可以使用下标表示法.

The last line can also be written using subscript notation.

let substring = original[newStartIndex..<newEndIndex]

这篇关于在 Swift 中从字符串中删除第一个字符的最简洁方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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