Substring/Top and tail: Difference between revisions
Content added Content deleted
Line 1,089: | Line 1,089: | ||
012345678 |
012345678 |
||
12345678</pre> |
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: |
The other method is slicing by range subscripting: |
||
<lang swift>let txt = "0123456789" |
<lang swift>let txt = "0123456789" |
||
Line 1,107: | Line 1,098: | ||
012345678 |
012345678 |
||
12345678</pre> |
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: |
Another way is mutating the string: |
||
<lang swift>var txt = "0123456789" |
<lang swift>var txt = "0123456789" |
||
txt.removeAtIndex(txt.startIndex) |
txt.removeAtIndex(txt.startIndex) |
||
txt.removeAtIndex(txt.endIndex.predecessor())</lang> |
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. |
The above functions return what they remove. |
||
You can also extend String type and define BASIC-style functions: |
You can also extend String type and define BASIC-style functions: |
||
<lang swift>extension String { |
<lang swift>extension String { |
||
/** Positive numbers give index from start of text, whilst negative numbers give index from end of text */ |
|||
/// Ensure positive indexes |
|||
⚫ | |||
⚫ | |||
private func positive(index: Int) -> Int { |
|||
if index >= 0 { return index } |
|||
⚫ | |||
} |
} |
||
/// Unicode character by zero-based integer (character) `index` |
|||
⚫ | |||
/// Supports negative character index to count from end. (-1 returns character before last) |
|||
⚫ | |||
⚫ | |||
} |
|||
/// String slice by character index |
|||
subscript(range: Range<Int>) -> String { |
|||
return self[advance(startIndex, range.startIndex) ..< |
|||
advance(startIndex, range.endIndex, endIndex)] |
|||
} |
|||
⚫ | |||
func left(index : Int) -> String { |
func left(index : Int) -> String { |
||
return self. |
return self[0 ..< positive(index)] |
||
} |
} |
||
/ |
/// Right portion of text from `index` |
||
func right(index : Int) -> String{ |
func right(index : Int) -> String{ |
||
⚫ | |||
return self[positive(index) ..< count(self)] |
|||
} |
} |
||
/ |
/// From `start` index until `end` index |
||
func mid(start: Int, _ end: Int) -> String { |
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)] |
|||
} |
} |
||