String extensions are a set of functions that allow you to extend the functionality of strings in your code. They can be used for things like internationalization, URL encoding and more.
C# extension methods are a set of useful string extensions that can be used to make coding in C# more efficient.
A collection of string extensions that may be used in a variety of situations.
Requirements
Installation
CocoaPods
CocoaPods is where you can get Extendy. Simply add the following line to your Podfile to install it:
‘> 1.1’, pod ‘Extendy’
Swift Package Manager is a program that allows you to manage your
The Swift Package Manager, which is built into the Swift compiler, is a tool for automating the distribution of Swift code.
Once you’ve built up your Swift package, adding Extendy as a dependent is as simple as adding it to the Package’s dependencies field. swift.
dependencies: [.package(url: “https://github.com/NovichenkoAnton/Extendy.git”,.upToNextMajor(from: “1.1”),.upToNextMajor(from: “1.1”),.upToNextMajor(from: “1.1”),.upToNextMajor(from: “1.1”),.upToNextMajor(from: “1.1”),.upToNextM
Usage
String
- Apply masks to a string within a certain range. CountableRange, ClosedRange, PartialRangeFrom, PartialRangeThrough, and PartialRangeUpTo are all compatible with this range.
let cardNumber = “1234567890123456” let cardNumber = “1234567890123456” let cardNumber = masked mask print(masked!) //”123456********56″ substring(in: 6…13, with: “*”)
- Convert a string into a double value. The method will return 0.00 if String is not compatible with Double.
amount = “100,12” print(amount.toDouble()) print(anotherAmount.toDouble()) /1200.1 let anotherAmount = “1 200,10”
- Strings may be formatted in a particular way.
Let’s say total equals “1234”. let sum.ext.apply = formattedSum print(formattedSum) format(.sum) /”1 234,00″
Formats that are supported
case creditCard case iban case custom(formatter: NumberFormatter) case custom(formatter: NumberFormatter) case custom(formatter: NumberFormatter) case custom(formatter: NumberFormatter) case custom(formatter: NumberFormatter) case custom(formatter: NumberFormatter) case custom(formatter: NumberFormatter) case custom(formatter: NumberFormatter) case custom(formatter: NumberFormatter) case custom(formatter:
-
The Luhn method is used to validate a number of credit cards.
-
Validate the string against a predefined pattern.
validate(with:.website) /true “https://www.google.com”.validate(with:.website)
- Date formatter that is easy to use (from one format to another)
dateTime = “2019-11-22 13:33” let dateTime = “2019-11-22 13:33” dateTime.ext.convertDate; let resultTime = dateTime.ext.convertDate; (from: “yyyy-MM-dd HH:mm”, to: “h:mm”) /”1:33″
- Retrieve query elements from a String that matches the URL type. URLs with cyrillic domain names are supported.
let queryItems = stringURL.queryItems()! /[“foo”: “1”, “bar”: “abc”] let stringURL = “https://test.com?foo=1&bar=abc”
NSMutableAttributedString
- + can be used to join two attributed strings.
Let part1 equal “123” and part2 equal “456.” myLabel.attributed part1.attributed + part2.attributed = text
- For changeable strings, apply attributes.
label.attributed let string = “Some text” string.attributed.apply = text Attributes([.color(color:.red),.font(font:.systemFont(ofSize: 32, weight:.bold)),.crossed(width: 1, color:.black),.underline(style:.single, color:.blue) ])
- String styles should be applied.
label.attributed = “1000,22” let sum = “1000,22” sum.attributed.apply = sum.attributed.apply = sum.attributed.apply = sum. Style(.sum(integer [.color(color: UIColor.red),.font(font: UIFont.systemFont(ofSize: 32, weight:.bold)),.underline(style:.double, color:.black)], fraction [.color(color: UIColor.green),.font(font: UIFont.systemFont(ofSize: 24, weight:.medium)), currencyMark: “$”)])
GitHub
https://github.com/NovichenkoAnton/Extendy
The iOS 11.3 is the latest update to iOS, and it includes a number of new features and improvements. Reference: swift string extension subscript.
Related Tags
- string extensions c#
- linq extension methods
- swift string extension
- c# extension file
- c# string extension methods library