presto95 / AGString

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AGString Logo

Swift 5.0 Version Platform Carthage Compatible SPM

AGString

AGString is an extension for convenient working with swift String.

AGRegex is Wrapper of NSRegularExpression. It gives an easy and iterative way to use RegularExpression.

Features

  • AGString gives 'String type' to 'Int-Index' based referencing

    • ✅ Get Character object with Int index
    • ✅ Get substring object with Range
    • ✅ String utility methods(zfill, countOccurence, ltrim,rtrim ...)
  • AGRegex

    • ✅ Provide NSRange extension for String
    • ✅ Get All Match Result as AGMatch structure.
    • ✅ Support iterative way to use match result
    • ✅ Substitute matched string to other string

Example

The example application is the best way to see AGString in action. Simply open the AGString.xcodeproj and run the Example scheme.

Installation

CocoaPods

AGString is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'AGString'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate AGString into your Xcode project using Carthage, specify it in your Cartfile:

github "SwiftAlgorithmClub/AGString"

Run carthage update to build the framework and drag the built AGString.framework into your Xcode project. On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase” and add the Framework path as mentioned in Carthage Getting started Step 4, 5 and 6

AGString Usage

Slicing

var str = "Hello, World!"
str[1..<3]  // "el"
str[7...9]  // "Wor"
str[7...]   // "World!"
str[..<5]   // "Hello"
str[...5]   // "Hello,"

CharAt

var str = "Hello, World!"
str[1] // "e"

Trim

"     abcd    ".trimLeft()
// "abcd    "
"     abcd    ".trimRight() 
// "     abcd"
"     abcd    ".trim()
//"abcd"

WordCount

let str = "like in like"
str.occurence(of: "like")   //  2

Zfill

let str = "abc"
str.zfill(10)                                 // "0000000abc"
str.zfill(10, with: "1", direction: .right)   // "abc1111111"

AGRegex Usage

FindAll

let r = try! NSRegularExpression(pattern: "ai", options: [])
let regex = AGRegex(r)
regex.findAll("The rain in Spain")
/* 
[
  AGMatch(start: 5, end: 7, base: str, groups: ["ai"]),
  AGMatch(start: 14, end: 16, base: str, groups: ["ai"]),
]
*/

First

let r = try! NSRegularExpression(pattern: "ai", options: [])
let regex = AGRegex(r)
regex.first("The rain in Spain")
// AGMatch(start: 5, end: 7, base: str, groups: ["ai"])

Last

let r = try! NSRegularExpression(pattern: "ai", options: [])
let regex = AGRegex(r)
regex.last("The rain in Spain")
// AGMatch(start: 14, end: 16, base: str, groups: ["ai"])

Sub

let r = try! NSRegularExpression(pattern: "ai", options: [])
let regex = AGRegex(r)
regex.last("The rain in Spain")
// AGMatch(start: 14, end: 16, base: str, groups: ["ai"])
let r = try! NSRegularExpression(pattern: "\\s", options: [])
let regex = AGRegex(r)
regex.sub(str: "The rain in Spain", replace: "9")
// The9rain9in9Spain
regex.sub(str: str, replace: "9", count: 2)
subWithCount == "The9rain9in Spain"
// "The9rain9in Spain"

FindIter

let r = try! NSRegularExpression(pattern: "([A-Z]+)([0-9]+)", options: [])
let regex = AGRegex(r)
for m in regex.finditer("ABC12DEF3G56HIJ7") {
   print("\(m.group(2)) * \(m.group(1))")
}
/*
 "12 * ABC",
 "3 * DEF",
 "56 * G",
 "7 * HIJ"
 */

Contributing

Contributions are very welcome 🙌

License

AGString is released under the MIT license. See LICENSE for details.

About

License:MIT License


Languages

Language:Swift 85.9%Language:Ruby 14.1%