From ecc1c0903ae2c799690b2fc931c18b40d28b8a1e Mon Sep 17 00:00:00 2001 From: Enix Yu Date: Sat, 8 Oct 2016 09:54:14 +0800 Subject: [PATCH 1/3] add rindexOf to find the last occurence of substring --- Pod/Classes/StringExtensions.swift | 7 +++++++ README.md | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/Pod/Classes/StringExtensions.swift b/Pod/Classes/StringExtensions.swift index 218d426..66027d0 100644 --- a/Pod/Classes/StringExtensions.swift +++ b/Pod/Classes/StringExtensions.swift @@ -112,6 +112,13 @@ public extension String { } return nil } + + func rindexOf(substring: String) -> Int? { + if let range = rangeOfString(substring, options: .BackwardsSearch) { + return startIndex.distanceTo(range.startIndex) + } + return nil + } func initials() -> String { let words = self.componentsSeparatedByString(" ") diff --git a/README.md b/README.md index 61981d4..43acf4d 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,13 @@ import SwiftString "hello".indexOf("world") // nil ``` +**rindexOf(substring)** +```swift +"hello".rindexOf("hell"), // 0 +"helloll".rindexOf("ll"), // 5 +"hello".rindexOf("world") // nil +``` + **initials()** ```swift "First".initials(), // "F" From c987180a7998ebacc408a0b6b79069bb6d98a27d Mon Sep 17 00:00:00 2001 From: Enix Yu Date: Sat, 8 Oct 2016 09:58:04 +0800 Subject: [PATCH 2/3] add test case for rindexOf --- Example/Tests/StringExtensionTests.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Example/Tests/StringExtensionTests.swift b/Example/Tests/StringExtensionTests.swift index 2c26d19..5d7b6e2 100644 --- a/Example/Tests/StringExtensionTests.swift +++ b/Example/Tests/StringExtensionTests.swift @@ -73,6 +73,13 @@ class SwiftStringTests: XCTestCase { assertThat("hello".indexOf("lo"), presentAnd(equalTo(3))) assertThat("hello".indexOf("world"), nilValue()) } + + func testRindexOf() { + assertThat("hello".rindexOf("hell"), presentAnd(equalTo(0))) + assertThat("helloll".rindexOf("ll"), presentAnd(equalTo(5))) + assertThat("hellolloll".rindexOf("ll"), presentAnd(equalTo(8))) + assertThat("hello".rindexOf("world"), nilValue()) + } func testInitials() { assertThat("First".initials() == "F") From ab345e8554b9882dc1d70f6a943d678eb77fd608 Mon Sep 17 00:00:00 2001 From: Enix Yu Date: Thu, 24 Nov 2016 12:26:45 +0800 Subject: [PATCH 3/3] add find substring function --- Example/Tests/StringExtensionTests.swift | 6 ++++++ Pod/Classes/StringExtensions.swift | 18 ++++++++++++++++++ README.md | 7 +++++++ 3 files changed, 31 insertions(+) diff --git a/Example/Tests/StringExtensionTests.swift b/Example/Tests/StringExtensionTests.swift index 5d7b6e2..6917c4e 100644 --- a/Example/Tests/StringExtensionTests.swift +++ b/Example/Tests/StringExtensionTests.swift @@ -174,6 +174,12 @@ class SwiftStringTests: XCTestCase { assertThat("hi".times(3) == "hihihi") assertThat(" ".times(10) == " ") } + + func testFindSubstring() { + assertThat("hello-world-abc".findSubstring("\\w+\\-\\w+", ignoreCase: false) == "hello-world") + assertThat("hello-world-abc".findSubstring("HELLO-world", ignoreCase: true) == "hello-world") + assertThat("hello-world-abc".findSubstring("\\d+\\-\\d+", ignoreCase: false) == nil) + } func testTrimmedLeft() { assertThat(" How are you? ".trimmedLeft() == "How are you? ") diff --git a/Pod/Classes/StringExtensions.swift b/Pod/Classes/StringExtensions.swift index 66027d0..2009f9d 100644 --- a/Pod/Classes/StringExtensions.swift +++ b/Pod/Classes/StringExtensions.swift @@ -252,6 +252,24 @@ public extension String { func toDateTime(format: String = "yyyy-MM-dd HH:mm:ss") -> NSDate? { return toDate(format) } + + func findSubstring(regexpStr: String, ignoreCase: Bool) -> String? { + var regExp: NSRegularExpression + if ignoreCase { + regExp = try! NSRegularExpression(pattern: regexpStr, options: .CaseInsensitive) + } else { + regExp = try! NSRegularExpression(pattern: regexpStr, options: NSRegularExpressionOptions(rawValue: 0)) + } + + let matches = regExp.matchesInString(self, options: NSMatchingOptions(rawValue: 0), range: NSRange.init(location: 0, length: self.characters.count)) + if matches.count > 0 { + let range = matches.first!.range + let result = self.substring(range.location, length: range.length) + return result + } else { + return nil + } + } func trimmedLeft() -> String { if let range = rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet().invertedSet) { diff --git a/README.md b/README.md index 43acf4d..10cf1b6 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,13 @@ import SwiftString "2".toDouble() // 2.0 ``` +**findSubstring()** +```swift +"hello-world-abc".findSubstring("\\w+\\-\\w+", ignoreCase: false) +"hello-world-abc".findSubstring("HELLO-world", ignoreCase: true) +"hello-world-abc".findSubstring("\\d+\\-\\d+", ignoreCase: false) +``` + **trimmedLeft()** ```swift " How are you? ".trimmedLeft() // "How are you? "