Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dec27c8

Browse files
Add containsMatch method
1 parent b3482cc commit dec27c8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

‎ExSwift/String.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ public extension String {
113113
return nil
114114
}
115115

116+
/**
117+
Check is string with this paaternt included in string
118+
119+
:param: pattern Pattern to match
120+
:param: ignoreCase true for case insensitive matching
121+
:returns: true if contains match, otherwise false
122+
*/
123+
func containsMatch (pattern: String, ignoreCase: Bool = false) -> Bool? {
124+
if let regex = ExSwift.regex(pattern, ignoreCase: ignoreCase) {
125+
let range = NSMakeRange(0, count(self))
126+
return regex.firstMatchInString(self, options: .allZeros, range: range) != nil
127+
}
128+
129+
return nil
130+
}
131+
116132
/**
117133
Inserts a substring at the given index in self.
118134

‎ExSwiftTests/StringExtensionsTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ class StringExtensionsSpec: QuickSpec {
159159

160160
}
161161

162+
163+
/**
164+
* String contains matches
165+
*/
166+
describe("containsMatch checks"){
167+
it("match") {
168+
expect("Test string for match".containsMatch("for")).to(beTrue())
169+
}
170+
it("not match") {
171+
expect("Test string for match".containsMatch("not for")).to(beFalse())
172+
}
173+
}
174+
162175
/**
163176
* String trimming methods
164177
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /