Substring/Top and tail: Difference between revisions

Line 1,089:
012345678
12345678</pre>
These functions are generic, not overloads:
<lang swift>let array = [0,1,2,3,4,5,6,7,8,9]
println(dropFirst(array))
println(dropLast(array))
println(dropFirst(dropLast(array)))</lang>
{{out}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]</pre>
The other method is slicing by range subscripting:
<lang swift>let txt = "0123456789"
Line 1,107 ⟶ 1,098:
012345678
12345678</pre>
This method is also generic:
<lang swift>let txt = "0123456789"
let array = [0,1,2,3,4,5,6,7,8,9]
 
func sliceDemo <T: Sliceable where T.Index: BidirectionalIndexType> (col: T) {
 
println(col[col.startIndex.successor() ..< col.endIndex])
println(col[col.startIndex ..< col.endIndex.predecessor()])
println(col[col.startIndex.successor() ..< col.endIndex.predecessor()])
}
 
sliceDemo(txt)
sliceDemo(array)</lang>
{{out}}
<pre>123456789
012345678
12345678
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]</pre>
Another way is mutating the string:
<lang swift>var txt = "0123456789"
txt.removeAtIndex(txt.startIndex)
txt.removeAtIndex(txt.endIndex.predecessor())</lang>
Although the above mutating functions are not part of a generic protocol (yet, as of Swift 1.2), similar functions exist for relevant collection types:
<lang swift>var array = [0,1,2,3,4,5,6,7,8,9]
array.removeAtIndex(0)
array.removeLast()</lang>
The above functions return what they remove.
You can also extend String type and define BASIC-style functions:
<lang swift>extension String {
/** Positive numbers give index from start of text, whilst negative numbers give index from end of text */
/// Ensure positive indexes
func index(index: Int) -> String.Index {
return advance( index < 0 ? self.endIndex : self.startIndex, index)
private func positive(index: Int) -> Int {
if index >= 0 { return index }
return self.substringToIndexcount(self.) + index(index))
}
/// Unicode character by zero-based integer (character) `index`
/** Left portion of text to index */
/// Supports negative character index to count from end. (-1 returns character before last)
func indexsubscript(index: Int) -> String.IndexCharacter {
return self[advance( index < 0 ? self.endIndex : self.startIndex, positive(index))]
}
/// String slice by character index
subscript(range: Range<Int>) -> String {
return self[advance(startIndex, range.startIndex) ..<
advance(startIndex, range.endIndex, endIndex)]
}
/**// Left portion of text to `index */`
func left(index : Int) -> String {
return self[0 .substringFromIndex(self.index< positive(index))]
}
 
/**// Right portion of text from `index */`
func right(index : Int) -> String{
return self.substringToIndex(self.index(index))
return self[positive(index) ..< count(self)]
}
/**// From left`start` index until right`end` index */
func mid(start: Int, _ end: Int) -> String {
return self.substringWithRange(Range<String.Index>(start: self.index(start), end: self.index(end)))
return self[positive(start) ..< positive(end)]
}
Anonymous user