A selection of algorithms that work with Strings
Often familiarity makes us turn back to the Cocoa Framework, but Swift has a wealth of algorithms that we can use with String. These include:
distance(str.startIndex, str.endIndex) // string length
count(str) // string length
str[advance(str.startIndex, 4)] // get character at index 4
str[advance(str.startIndex, 4)...advance(str.startIndex, 8)] // get characters in range index 4 to 8
last(str) // retrieve last letter
first(str) // retrieve first letter
dropFirst(str) // remove first letter
dropLast(str) // remove last letter
filter(str, {!contains("aeiou", $0)}) // remove vowels
indices(str) // retrieve the Range value for string
isEmpty(str) // test whether there is anything in the string
minElement(indices(str)) // first index
str.substringToIndex(advance(minElement(indices(str)), 5)) // returns string up to the 5th character
str.substringFromIndex(advance(minElement(indices(str)), 5)) // returns string from the 5th character
min("antelope","ant") // returns the alphabetical first
max("antelope","ant") // returns the alphabetical last
prefix(str, 5) // returns first 5 characters
reverse(str) // return reverse array of Characters
suffix(str, 5) // returns last 5 characters
swap(&str, &a) // swaps two strings for the value of one another
Update: Swift 2 (string algorithms)
var str = "Hello Swift!"
var aStr = "Hello World!"
str.startIndex // first index
str.endIndex // end index
distance(str.startIndex, str.endIndex) // string length
str.characters.count // string length
str[advance(str.startIndex, 4)] // get character at index 4
str[advance(str.startIndex, 4)...advance(str.startIndex, 8)] // get characters in range index 4 to 8
str.characters.last // retrieve last letter
str.characters.first // retrieve first letter
str.removeAtIndex(str.characters.indices.first!) // remove first letter
str.removeAtIndex(str.characters.indices.last!) // remove last letter
let a = dropFirst(str.characters)
String(a) // dropFirst
let a = dropLast(str.characters)
String(a) // dropLast
"aeiou".characters.contains("a") // contains character
str.characters.filter{!"aeiou".characters.contains($0)} // remove vowels
str.characters.indices // retrieve the Range value for string
str.isEmpty // test whether there is anything in the string
advance(str.startIndex, 5) // advance index
str.substringToIndex(advance(str.startIndex,5)) // returns string up to the 5th character
str.substringFromIndex(advance(str.startIndex,5)) // returns string from the 5th character
min("antelope","ant") // returns the alphabetical first
max("antelope","ant") // returns the alphabetical last
prefix(str.characters, 5) // returns first 5 characters
str.characters.reverse() // return reverse array of Characters
suffix(str.characters, 5) // returns last 5 characters
swap(&str, &aStr) // swaps two strings for the value of one another
Reversing a string
Things start to get interesting when you start to combine these algorithms, like so
var str = "hello"
var revStr = ""
for i in str {
revStr.append(last(str)!)
str = dropLast(str)
}
revStr // "olleh"
Update: Swift 2 (reversing a string)
var str = "Hello Swift!"
String(str.characters.reverse()) // "!tfiwS olleH"
in this process for reversing a string.
rangesOfString:
It is also possible to go further and remove the need for Cocoa Framework methods like rangeOfString. And here I'm doing something very similar by retrieving the ranges of a string
extension String {
func rangesOfString(findStr:String) -> [Range] {
var arr = [Range]()
var startInd = self.startIndex
// check first that the first character of search string exists
if contains(self, first(findStr)!) {
// if so set this as the place to start searching
startInd = find(self,first(findStr)!)!
}
else {
// if not return empty array
return arr
}
var i = distance(self.startIndex, startInd)
while i<=count(self)-count(findStr) {
if self[advance(self.startIndex, i)..< hello?.rangesOfString(?hello”) hello, good very ?a find every after character search first of index next to jumping by optimisation further try } arr return i++ { else i="i+count(findStr)" i+count(findStr)))) i),end:advance(self.startIndex, arr.append(Range(start:advance(self.startIndex, findStr i+count(findStr))]="=">
using a String extension written entirely in Swift with no added Cocoa.
Update: Swift 2 implementation (rangesOfString:)
Note that not only are instance methods are now used as opposed to functions, but find() is replaced by indexOf().
extension String {
func rangesOfString(findStr:String) -> [Range] {
var arr = [Range]()
var startInd = self.startIndex
// check first that the first character of search string exists
if self.characters.contains(findStr.characters.first!) {
// if so set this as the place to start searching
startInd = self.characters.indexOf(findStr.characters.first!)!
}
else {
// if not return empty array
return arr
}
var i = distance(self.startIndex, startInd)
while i<=self.characters.count-findStr.characters.count {
if self[advance(self.startIndex, i)..< hello, good very ?a find every after character search first of index next to jumping by optimisation further try } arr return i++ { else i="i+findStr.characters.count" i),end:advance(self.startIndex, arr.append(Range(start:advance(self.startIndex, findStr hello?.rangesOfString(?hello?) i+findStr.characters.count))) i+findStr.characters.count)]="=">
Source: sketchytech
No comments:
Post a Comment