String 在Swift中,从字符串中删除第一个字符最简洁的方法是什么?
我想从字符串中删除第一个字符。到目前为止,我想到的最简洁的东西是:String 在Swift中,从字符串中删除第一个字符最简洁的方法是什么?,string,swift,String,Swift,我想从字符串中删除第一个字符。到目前为止,我想到的最简洁的东西是: display.text = display.text!.substringFromIndex(advance(display.text!.startIndex, 1)) 我知道,由于Unicode,我们无法将Int索引到字符串中,但这个解决方案似乎非常冗长。还有其他我忽略的方法吗?我知道没有比开箱即用更简洁的方法了,但是你可以很容易地实现前缀++,例如 public prefix func ++ <I: Forward
display.text = display.text!.substringFromIndex(advance(display.text!.startIndex, 1))
我知道,由于Unicode,我们无法将
Int
索引到字符串中,但这个解决方案似乎非常冗长。还有其他我忽略的方法吗?我知道没有比开箱即用更简洁的方法了,但是你可以很容易地实现前缀++
,例如
public prefix func ++ <I: ForwardIndexType>(index: I) -> I {
return advance(index, 1)
}
这个怎么样
s.removeAtIndex(s.startIndex)
当然,这假设字符串是可变的。它返回已删除的字符,但会更改原始字符串。如果使用的是Swift 3,则可以忽略此答案的第二部分。好消息是,这现在其实又是简洁了!只需使用String的新remove(at:)方法
我喜欢全局
dropFirst()
函数
let original = "Hello" // Hello
let sliced = dropFirst(original) // ello
它简短、清晰,适用于符合切片协议的任何内容
如果您使用的是Swift 2,则此答案已更改。您仍然可以使用dropFirst,但必须先从stringscharacters
属性中删除第一个字符,然后将结果转换回字符串。dropFirst也成为了一种方法,而不是一个函数
let original = "Hello" // Hello
let sliced = String(original.characters.dropFirst()) // ello
另一种选择是使用后缀函数拼接字符串的UTF16View
。当然,之后也必须将其转换回字符串
let original = "Hello" // Hello
let sliced = String(suffix(original.utf16, original.utf16.count - 1)) // ello
所有这一切都意味着,我最初提供的解决方案在更新版本的Swift中并不是最简洁的方法。如果您正在寻找一个简短直观的解决方案,我建议您使用removatendex()
使用@chris'解决方案
var original = "Hello" // Hello
let removedChar = original.removeAtIndex(original.startIndex)
original // ello
正如@vacawama在下面的注释中指出的,另一个不修改原始字符串的选项是使用substringFromIndex
let original = "Hello" // Hello
let substring = original.substringFromIndex(advance(original.startIndex, 1)) // ello
或者,如果您碰巧希望从字符串的开头和结尾删除一个字符,则可以使用substringWithRange。只要确保在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
让original=“Hello”//Hello
让newStartIndex=advance(original.startIndex,1)
让newEndIndex=advance(original.endIndex,-1)
让substring=original.substringWithRange(newStartIndex..为Swift 4更新
extension String {
func chopPrefix(_ count: Int = 1) -> String {
if count >= 0 && count <= self.count {
let indexStartOfText = self.index(self.startIndex, offsetBy: count)
return String(self[indexStartOfText...])
}
return ""
}
func chopSuffix(_ count: Int = 1) -> String {
if count >= 0 && count <= self.count {
let indexEndOfText = self.index(self.endIndex, offsetBy: -count)
return String(self[..<indexEndOfText])
}
return ""
}
}
在Swift 4中,String
再次符合Collection
,因此可以使用dropFirst
和dropLast
修剪字符串的开头和结尾。结果是类型Substring
,因此您需要将其传递给String
构造函数以获取字符串
:
let str = "hello"
let result1 = String(str.dropFirst()) // "ello"
let result2 = String(str.dropLast()) // "hell"
extension String {
func chopPrefix(_ count: Int = 1) -> String {
return substring(from: index(startIndex, offsetBy: count))
}
func chopSuffix(_ count: Int = 1) -> String {
return substring(to: index(endIndex, offsetBy: -count))
}
}
"hello".chopPrefix() // "ello"
"hello".chopPrefix(3) // "lo"
"hello".chopSuffix() // "hell"
"hello".chopSuffix(3) // "he"
dropFirst()
和dropLast()
也使用Int
指定要删除的字符数:
let result3 = String(str.dropLast(3)) // "he"
let result4 = String(str.dropFirst(4)) // "o"
如果指定要删除的字符多于字符串中的字符,则结果将是空字符串(“”
)
Swift 3的更新
如果您只想删除第一个字符并更改原始字符串,请参阅@MickMacCallum的答案。如果您想在此过程中创建新字符串,请使用子字符串(from:)
。通过扩展到字符串
,您可以隐藏子字符串(from:)
和子字符串(to:)的丑陋之处
要创建有用的附加内容来修剪字符串的开头和结尾
:
let str = "hello"
let result1 = String(str.dropFirst()) // "ello"
let result2 = String(str.dropLast()) // "hell"
extension String {
func chopPrefix(_ count: Int = 1) -> String {
return substring(from: index(startIndex, offsetBy: count))
}
func chopSuffix(_ count: Int = 1) -> String {
return substring(to: index(endIndex, offsetBy: -count))
}
}
"hello".chopPrefix() // "ello"
"hello".chopPrefix(3) // "lo"
"hello".chopSuffix() // "hell"
"hello".chopSuffix(3) // "he"
像前面的dropFirst
和dropLast
一样,如果字符串中没有足够的可用字母,这些函数将崩溃。调用者有责任正确使用它们。这是一个有效的设计决策。可以编写它们以返回一个可选值,然后调用者必须打开该值
Swift 2.x
唉,在Swift 2中,dropFirst
和dropLast
(以前最好的解决方案)不像以前那样方便。通过对字符串的扩展,您可以隐藏substringFromIndex
和substringToIndex
的丑陋之处:
extension String {
func chopPrefix(count: Int = 1) -> String {
return self.substringFromIndex(advance(self.startIndex, count))
}
func chopSuffix(count: Int = 1) -> String {
return self.substringToIndex(advance(self.endIndex, -count))
}
}
"hello".chopPrefix() // "ello"
"hello".chopPrefix(3) // "lo"
"hello".chopSuffix() // "hell"
"hello".chopSuffix(3) // "he"
像前面的dropFirst
和dropLast
一样,如果字符串中没有足够的可用字母,这些函数将崩溃。调用者有责任正确使用它们。这是一个有效的设计决策。可以编写它们以返回一个可选值,然后调用者必须打开该值
在Swift 1.2中,您需要按如下方式调用chopRefix
:
"hello".chopPrefix(count: 3) // "lo"
或者,您可以在函数定义中添加下划线\uu
,以抑制参数名称:
extension String {
func chopPrefix(_ count: Int = 1) -> String {
return self.substringFromIndex(advance(self.startIndex, count))
}
func chopSuffix(_ count: Int = 1) -> String {
return self.substringToIndex(advance(self.endIndex, -count))
}
}
在Swift 2中,执行以下操作:
let cleanedString = String(theString.characters.dropFirst())
我建议您了解Swift字符串。在Swift 2中使用以下字符串扩展名:
extension String
{
func substringFromIndex(index: Int) -> String
{
if (index < 0 || index > self.characters.count)
{
print("index \(index) out of bounds")
return ""
}
return self.substringFromIndex(self.startIndex.advancedBy(index))
}
}
display.text = display.text!.substringFromIndex(1)
扩展字符串
{
func substringFromIndex(索引:Int)->字符串
{
if(索引<0 | |索引>self.characters.count)
{
打印(“索引\(索引)超出范围”)
返回“”
}
返回self.substringFromIndex(self.startIndex.advancedBy(索引))
}
}
display.text=display.text!。substringfromfromindex(1)
Swift 2.2
“advance”不可用:调用索引上的“advancedBy(n)”方法
Swift 3.0
func chopPrefix(_ count: Int = 1) -> String {
return self.substring(from: self.characters.index(self.startIndex, offsetBy: count))
}
func chopSuffix(_ count: Int = 1) -> String {
return self.substring(to: self.characters.index(self.endIndex, offsetBy: -count))
}
Swift 3.2
字符串内容作为字符集合的视图
@available(swift, deprecated: 3.2, message: "Please use String or Substring directly")
public var characters: String.CharacterView
func chopRefix(count:Int=1)->字符串{
如果计数>=0&&count字符串{
如果计数>=0&&count字符串{
如果计数>=0&&count字符串{
如果计数>=0&&count
“我们的,我们的,我们的”。切碎(5)。切碎(5)/“,我们的,我们的,”
从字符串中删除第一个字符
let choppedString = String(txtField.text!.characters.dropFirst())
迅捷3
这里是chopperfix
扩展的Swift4崩溃保存版本,将choppeffix
留给社区
extension String {
func chopPrefix(_ count: Int = 1) -> String {
return count>self.count ? self : String(self[index(self.startIndex, offsetBy: count)...])
}
}
前面的答案很好,但是到
func chopPrefix(_ count: Int = 1) -> String {
if count >= 0 && count <= self.count {
return self.substring(from: String.Index(encodedOffset: count))
}
return ""
}
func chopSuffix(_ count: Int = 1) -> String {
if count >= 0 && count <= self.count {
return self.substring(to: String.Index(encodedOffset: self.count - count))
}
return ""
}
extension String {
func chopPrefix(_ count: Int = 1) -> String {
if count >= 0 && count <= self.count {
let indexStartOfText = self.index(self.startIndex, offsetBy: count)
return String(self[indexStartOfText...])
}
return ""
}
func chopSuffix(_ count: Int = 1) -> String {
if count >= 0 && count <= self.count {
let indexEndOfText = self.index(self.endIndex, offsetBy: -count)
return String(self[..<indexEndOfText])
}
return ""
}
}
extension String {
func chopPrefix(count: Int = 1) -> String {
return self.substringFromIndex(self.startIndex.advancedBy(count))
}
func chopSuffix(count: Int = 1) -> String {
return self.substringToIndex(self.endIndex.advancedBy(-count))
}
}
let choppedString = String(txtField.text!.characters.dropFirst())
extension String {
func chopPrefix(_ count: UInt = 1) -> String {
return substring(from: characters.index(startIndex, offsetBy: Int(count)))
}
func chopSuffix(_ count: UInt = 1) -> String {
return substring(to: characters.index(endIndex, offsetBy: -Int(count)))
}
}
class StringChopTests: XCTestCase {
func testPrefix() {
XCTAssertEqual("original".chopPrefix(0), "original")
XCTAssertEqual("Xfile".chopPrefix(), "file")
XCTAssertEqual("filename.jpg".chopPrefix(4), "name.jpg")
}
func testSuffix() {
XCTAssertEqual("original".chopSuffix(0), "original")
XCTAssertEqual("fileX".chopSuffix(), "file")
XCTAssertEqual("filename.jpg".chopSuffix(4), "filename")
}
}
extension String {
func chopPrefix(_ count: Int = 1) -> String {
return count>self.count ? self : String(self[index(self.startIndex, offsetBy: count)...])
}
}
var line: String = "This is a string..."
var char: Character? = nil
char = line.removeFirst()
print("char = \(char)") // char = T
print("line = \(line)") // line = his is a string ...
var str = "hello"
str.removeFirst() // changes str
let str = "hello"
let strSlice = str.dropFirst() // makes a slice without the first letter
let str2 = String(strSlice)