Last active
October 10, 2017 17:25
-
-
Save khawajafarooq/0bf6f7d7924a8ed6085cc9cb75e9367a to your computer and use it in GitHub Desktop.
String extension for swift
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
import Foundation | |
extension String { | |
/* Get length of string */ | |
var length: Int { | |
return self.characters.count | |
} | |
/* Trim leading or trailing white spaces */ | |
var trim: String { | |
return self.trimmingCharacters(in: .whitespaces) | |
} | |
/* Check if string contains a single numeric digit */ | |
var isSingleNumericDigit: Bool { | |
if self.length != 1 { | |
return false | |
} | |
else { | |
let numberPredicate: NSPredicate = NSPredicate(format: "SELF MATCHES '^[0-9]+$'", 1) | |
return numberPredicate.evaluate(with: self) | |
} | |
} | |
} |
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
var str = " Hello, playground " | |
str.length | |
str.trim | |
str.isSingleNumericDigit | |
str = "c" | |
str.isSingleNumericDigit | |
str = "1" | |
str.isSingleNumericDigit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment