Created
January 9, 2017 06:13
-
-
Save yossan/1594b50055fa5d5e51dba688c9ff02ec to your computer and use it in GitHub Desktop.
[swift3] String search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func first(_ search: String) -> Int { | |
var hit: Int = -1 | |
next: for i in 0..<(self.characters.count - search.characters.count) { | |
for j in 0..<search.characters.count { | |
let n = self.characters[self.index(self.startIndex, offsetBy: (i+j))] | |
let s = search.characters[search.index(search.startIndex, offsetBy: j)] | |
if n != s { | |
break | |
} else if j == search.characters.count - 1 { | |
hit = i | |
break next | |
} | |
} | |
} | |
return hit | |
} | |
} | |
let r = "๐ฎ๐๐ถ๐พ๐๐ฎ๐ท๐ด".first("๐พ") | |
print("r = \(r)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment