找到你要的答案

Q:Understanding the removeRange(_:) documentation

Q:了解removerange(_:)文件

To remove a substring at a specified range, use the removeRange(_:) method:

1 let range = advance(welcome.endIndex, -6)..<welcome.endIndex
2 welcome.removeRange(range)
3 println(welcome)
4 // prints "hello"

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/ca/jEUH0.l

Hi there,

I don't fully understand the syntax and the function of line 1 in the code above.

Please explain using this string:

let welcome = "hello there"

This is what I've worked out:

"To change the start and end index, use advance()."
From: http://stackoverflow.com/a/24045156/4839671

A better documentation of advance() is welcomed. i.e. it's arguments

Use ..< to make a range that omits its upper value

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/ca/jEUH0.l

welcome.endIndex will be 11

To remove a substring at a specified range, use the removeRange(_:) method:

1 let range = advance(welcome.endIndex, -6)..<welcome.endIndex
2 welcome.removeRange(range)
3 println(welcome)
4 // prints "hello"

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/ca/jEUH0.l

你好,

我不完全理解上面代码中的第1行的语法和函数。

请用这个字符串解释:

let welcome = "hello there"

这就是我所做出来的:

"To change the start and end index, use advance()."
From: http://stackoverflow.com/a/24045156/4839671

更好的文档的advance()欢迎。即它的论点

使用。<;做一个范围,避免了其上限值

摘自:苹果的iBooks的Swift编程语言。”。http:/ /没有。ES /钙/ jeuh0。l

welcome.endindex将11

answer1: 回答1:

Swift 2

We're going to use var since removeRange needs to operate on a mutable string.

var welcome = "hello there"

This line:

let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex

means that we start from the end of the string (welcome.endIndex) and move back by 6 characters (advance by a negative number = move back), then ask for the range (..<) between our position and the end of the string (welcome.endIndex).

It creates a range of 5..<11, which encompasses the " there" part of the string.

If you remove this range of characters from the string with:

welcome.removeRange(range)

then your string will be the remaining part:

print(welcome) // prints "hello"

You can take it the other way (from the start index of the string) for the same result:

welcome = "hello there"
let otherRange = welcome.startIndex.advancedBy(5)..<welcome.endIndex
welcome.removeRange(otherRange)
print(welcome) // prints "hello"

Here we start from the beginning of the string (welcome.startIndex), then we advance by 5 characters, then we make a range (..<) from here to the end of the string (welcome.endIndex).

Note: the advance function can work forward and backward.

Swift 3

The syntax has changed but the concepts are the same.

var welcome = "hello there"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
print(welcome) // prints "hello"

welcome = "hello there"
let otherRange = welcome.index(welcome.startIndex, offsetBy: 5)..<welcome.endIndex
welcome.removeSubrange(otherRange)
print(welcome) // prints "hello"

雨燕2

我们要用VaR自removerange需要一个可变的字符串操作。

var welcome = "hello there"

这条线:

let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex

意味着我们从字符串的结尾开始(欢迎。endIndex)和6个字符后移动(由负数=后退前进),然后要求范围(。<;)我们的位置和结束的字符串之间(欢迎。endIndex)。

它创建了一个范围为5 & LT;11,其中包括“有”部分的字符串。

如果从字符串中移除此字符范围:

welcome.removeRange(range)

然后你的字符串将是剩下的部分:

print(welcome) // prints "hello"

您可以采取其他方式(从开始索引的字符串)相同的结果:

welcome = "hello there"
let otherRange = welcome.startIndex.advancedBy(5)..<welcome.endIndex
welcome.removeRange(otherRange)
print(welcome) // prints "hello"

在这里,我们开始从字符串的开头(欢迎。指数),然后我们提前5个字,然后做一个范围(。<;)从这里到字符串的末尾(欢迎。endIndex)。

注意:先进的功能可以向前和向后工作。

雨燕3

语法已经改变,但概念是相同的。

var welcome = "hello there"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
print(welcome) // prints "hello"

welcome = "hello there"
let otherRange = welcome.index(welcome.startIndex, offsetBy: 5)..<welcome.endIndex
welcome.removeSubrange(otherRange)
print(welcome) // prints "hello"
swift