什么是从Swift的string中删除第一个字符最简洁的方法?
我想从string中删除第一个字符。 到目前为止,我提出的最简洁的东西是:
display.text = display.text!.substringFromIndex(advance(display.text!.startIndex, 1))  我知道我们不能用Int来索引一个string,因为Unicode,但是这个解决scheme看起来非常冗长。 有没有另外一种方法可以忽略? 
如果你使用Swift 3 ,你可以忽略这个答案的第二部分。 好消息是,现在这个又简洁了! 只需使用String的新的remove(at :)方法。
 var myString = "Hello, World" myString.remove(at: myString.startIndex) myString // "ello, World" 
 我喜欢这个全局的dropFirst()函数。 
 let original = "Hello" // Hello let sliced = dropFirst(original) // ello 
它简短明了,适用于符合Sliceable协议的任何东西。
  如果你使用Swift 2 ,这个答案已经改变了。 您仍然可以使用dropFirst,但是不能从stringcharacters属性中删除第一个字符,然后将结果转换回string。  dropFirst也成为一种方法,而不是一个function。 
 let original = "Hello" // Hello let sliced = String(original.characters.dropFirst()) // ello 
 另一种方法是使用后缀函数来拼接string的UTF16View 。 当然,这也必须转换回string。 
 let original = "Hello" // Hello let sliced = String(suffix(original.utf16, original.utf16.count - 1)) // ello 
 所有这一切都是说我最初提供的解决scheme并不是在较新版本的Swift中这样做的最简洁的方法。 如果您正在寻找一个简短而直观的解决scheme,我build议使用removeAtIndex()回退@chris的解决scheme。 
 var original = "Hello" // Hello let removedChar = original.removeAtIndex(original.startIndex) original // ello 
正如@vacawama在下面的评论中指出的那样,另一个不修改原始string的选项是使用substringFromIndex。
 let original = "Hello" // Hello let substring = original.substringFromIndex(advance(original.startIndex, 1)) // ello 
 或者如果您碰巧想要从string的开始和结尾删除字符,则可以使用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 
最后一行也可以使用下标符号来书写。
 let substring = original[newStartIndex..<newEndIndex] 
更新Swift 4
 在Swift 4中, String再次符合Collection ,因此可以使用dropFirst和dropLast来修剪string的开始和结束。 结果是types的Substring ,所以你需要将它传递给String构造函数来获取一个String : 
 let str = "hello" let result1 = String(str.dropFirst()) // "ello" let result2 = String(str.dropLast()) // "hell" 
  dropFirst()和dropLast()也采用Int来指定要放置的字符数量: 
 let result3 = String(str.dropLast(3)) // "he" let result4 = String(str.dropFirst(4)) // "o" 
 如果您指定的字符数多于string中的字符数,则结果将是空string( "" )。 
 let result5 = String(str.dropFirst(10)) // "" 
Swift 3的更新
 如果您只想删除第一个字符,并想要更改原始string,请参阅@ MickMacCallum的答案。 如果你想在这个过程中创build一个新的string,使用substring(from:) 。 通过对String的扩展,可以隐藏substring(from:)的丑substring(from:)和substring(to:)来创build有用的添加来修剪String的开始和结束: 
 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之前,如果string中没有足够的字母可用,这些函数将会崩溃。 主叫方有责任正确使用它们。 这是一个有效的devise决定。 人们可以写他们返回一个可选的,然后将不得不由打电话给解包。 
Swift 2.x
 唉,在Swift 2中 , dropFirst和dropLast (之前的最佳解决scheme)并不像以前那么方便。 通过对String的扩展,您可以隐藏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之前,如果string中没有足够的字母可用,这些函数将会崩溃。 主叫方有责任正确使用它们。 这是一个有效的devise决定。 人们可以写他们返回一个可选的,然后将不得不由打电话给解包。 
 在Swift 1.2中 ,你需要像这样调用chopPrefix : 
 "hello".chopPrefix(count: 3) // "lo" 
 或者你可以添加一个下划线_到函数定义来抑制参数名称: 
 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 2.2
'advance'不可用:在索引上调用'advancedBy(n)'方法
  func chopPrefix(count: Int = 1) -> String { return self.substringFromIndex(self.startIndex.advancedBy(count)) } func chopSuffix(count: Int = 1) -> String { return self.substringFromIndex(self.endIndex.advancedBy(count)) } 
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)) } 
斯威夫特4
作为字符集合的string内容的视图。
@available(swift, deprecated: 3.2, message: "Please use String or Substring directly") public var characters: String.CharacterView
 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 "" } 
那这个呢?
 s.removeAtIndex(s.startIndex) 
这当然假定你的string是可变的。 它返回已被删除的字符,但会改变原始string。
 我知道没有什么更简洁的方块,但你可以很容易地实现前缀++ ,例如, 
 public prefix func ++ <I: ForwardIndexType>(index: I) -> I { return advance(index, 1) } 
之后,您可以非常简洁地将它用于您的内容:
 str.substringFromIndex(++str.startIndex) 
在Swift 2中使用这个string扩展:
 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) 
“en_US,fr_CA,es_US”.chopSuffix(5).chopPrefix(5)//“,fr_CA,”
 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)) } } 
Swift3
 extension String { func chopPrefix(_ count: Int = 1) -> String { return substring(from: characters.index(startIndex, offsetBy: count)) } func chopSuffix(_ count: Int = 1) -> String { return substring(to: characters.index(endIndex, offsetBy: -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") } } 
从string中删除第一个字符
 let choppedString = String(txtField.text!.characters.dropFirst()) 
 这是一个Swift4崩溃保存版本的chopPrefix扩展,留下chopSuffix到社区… 
 extension String { func chopPrefix(_ count: Int = 1) -> String { return count>self.count ? self : String(self[index(self.startIndex, offsetBy: count)...]) } }