Created
October 16, 2019 14:51
-
-
Save mudasir093/47d9b041de4608d8f96588402af110ec to your computer and use it in GitHub Desktop.
Useful Sting Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Useful String Extension | |
//By Mudasir Syed | |
extension String { | |
// Extension for string length of String | |
var length: Int { | |
return self.count | |
} | |
// Extension for Subscripting till Specific index | |
subscript (i: Int) -> String { | |
return self[i ..< i + 1] | |
} | |
// Extension for HTML to Attribute String | |
var htmlToAttributedString: NSAttributedString? { | |
guard let data = data(using: .utf8) else { return NSAttributedString() } | |
do { | |
return try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) | |
} catch { | |
return NSAttributedString() | |
} | |
} | |
//OR | |
var htmlToAttributedString : NSAttributedString? { | |
do { | |
return try NSAttributedString(data: Data(utf8), | |
options: [.documentType: NSAttributedString.DocumentType.html, | |
.characterEncoding: String.Encoding.utf8.rawValue], | |
documentAttributes: nil) | |
} catch { | |
print("error: ", error) | |
return nil | |
} | |
} | |
// Extension for HTML to String | |
var htmlToString: String { | |
return htmlToAttributedString?.string ?? "" | |
} | |
//OR | |
var htmlToString: String { | |
return html2AttributedString?.string ?? "" | |
} | |
// Extension for compare string. | |
func isEqualToString(find: String) -> Bool { | |
return String(format: self) == find | |
} | |
// Extension for Base64 encoding a string | |
func base64Encoded() -> String? { | |
if let data = self.data(using: .utf8) { | |
return data.base64EncodedString() | |
} | |
return nil | |
} | |
// Extension for Base64 decoding a string | |
func base64Decoded() -> String? { | |
if let data = Data(base64Encoded: self) { | |
return String(data: data, encoding: .utf8) | |
} | |
return nil | |
} | |
// Extension for Replace Arabic Digit To English Digit | |
var replacedArabicDigitsWithEnglish: String { | |
var str = self | |
let map = ["٠": "0", | |
"١": "1", | |
"٢": "2", | |
"٣": "3", | |
"٤": "4", | |
"٥": "5", | |
"٦": "6", | |
"٧": "7", | |
"٨": "8", | |
"٩": "9"] | |
map.forEach { str = str.replacingOccurrences(of: $0, with: $1) } | |
return str | |
} | |
// Extension for Replace - Dash For Empty String | |
func setDashForEmpry() -> String{ | |
if self == "" { | |
return "-" | |
} | |
return self | |
} | |
// Extension for String Width | |
func widthOfString(usingFont font: UIFont) -> CGFloat { | |
let fontAttributes = [NSAttributedString.Key.font: font] | |
let size = self.size(withAttributes: fontAttributes) | |
return size.width | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment