I can find first position of string "ATG" in myString "ATGGACGTGAGCTGATCGATGGCTGAAATGAAAA" (i.e. index range is 0..<3) by using code below. Question is how to find all positions of "ATG", not only the first one in the myString.
let stringRange = myString.rangeOfString("ATG")
Swift String contains() The contains() method checks whether the specified string (sequence of characters) is present in the string or not.
In swift, we can use the firstIndex(of:) method to get the index position of a character in a given string.
A structure used to describe a portion of a series, such as characters in a string or objects in an array.
Substrings. When you get a substring from a string—for example, using a subscript or a method like prefix(_:) —the result is an instance of Substring , not another string. Substrings in Swift have most of the same methods as strings, which means you can work with substrings the same way you work with strings.
You can use NSRegularExpression
to find all occurrences of your string:
Swift 1.2:
let mystr = "ATGGACGTGAGCTGATCGATGGCTGAAATGAAAA"
let searchstr = "ATG"
let ranges: [NSRange]
// Create the regular expression.
if let regex = NSRegularExpression(pattern: searchstr, options: nil, error: nil) {
// Use the regular expression to get an array of NSTextCheckingResult.
// Use map to extract the range from each result.
ranges = regex.matchesInString(mystr, options: nil, range: NSMakeRange(0, count(mystr))).map {$0.range}
} else {
// There was a problem creating the regular expression
ranges = []
}
println(ranges) // prints [(0,3), (18,3), (27,3)]
Swift 2:
let mystr = "ATGGACGTGAGCTGATCGATGGCTGAAATGAAAA"
let searchstr = "ATG"
let ranges: [NSRange]
do {
// Create the regular expression.
let regex = try NSRegularExpression(pattern: searchstr, options: [])
// Use the regular expression to get an array of NSTextCheckingResult.
// Use map to extract the range from each result.
ranges = regex.matchesInString(mystr, options: [], range: NSMakeRange(0, mystr.characters.count)).map {$0.range}
}
catch {
// There was a problem creating the regular expression
ranges = []
}
print(ranges) // prints [(0,3), (18,3), (27,3)]
Swift 3: using Swift's native Range
type.
let mystr = "ATGGACGTGAGCTGATCGATGGCTGAAATGAAAA"
let searchstr = "ATG"
do {
// Create the regular expression.
let regex = try NSRegularExpression(pattern: searchstr, options: [])
// Use the regular expression to get an array of NSTextCheckingResult.
// Use map to extract the range from each result.
let fullStringRange = mystr.nsRange(from: mystr.startIndex ..< mystr.endIndex)
let matches = regex.matches(in: mystr, options: [], range: fullStringRange)
let ranges = matches.map {$0.range}
print(ranges) // prints [(0,3), (18,3), (27,3)]
}
catch {}
Notes:
"+*()[].{}?\^$"
) which have special meaning in a regular expression, then this will not work as expected. You could preprocess the search string to add escapes to nullify the special meanings of those characters, but this is probably more trouble than it is worth.mystr
is "AAAA"
and searchstr
is "AA"
. In this case, the string will only be found twice. The middle AA
will not be found because it starts with a character that is part of the first range.extension String {
public func rangesOfString(searchString:String, options: NSStringCompareOptions = [], searchRange:Range<Index>? = nil ) -> [Range<Index>] {
if let range = rangeOfString(searchString, options: options, range:searchRange) {
let nextRange = Range(start:range.endIndex, end:self.endIndex)
return [range] + rangesOfString(searchString, searchRange: nextRange)
} else {
return []
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With