Skip to content

Instantly share code, notes, and snippets.

View nitesh-iosdev's full-sized avatar

nitesh-iosdev

View GitHub Profile
@nitesh-iosdev
nitesh-iosdev / string-truncate.swift
Created March 18, 2018 17:37 — forked from jesskturner/string-truncate.swift
A little truncate function extension for the default String type
extension String {
/// Truncates the string to length number of characters and
/// appends optional trailing string if longer
func truncate(length: Int, trailing: String? = nil) -> String {
if self.characters.count > length {
return self.substringToIndex(self.startIndex.advancedBy(length)) + (trailing ?? "")
} else {
return self
}
}
@nitesh-iosdev
nitesh-iosdev / string-truncate.swift
Created March 18, 2018 17:37 — forked from jesskturner/string-truncate.swift
A little truncate function extension for the default String type
extension String {
/// Truncates the string to length number of characters and
/// appends optional trailing string if longer
func truncate(length: Int, trailing: String? = nil) -> String {
if self.characters.count > length {
return self.substringToIndex(self.startIndex.advancedBy(length)) + (trailing ?? "")
} else {
return self
}
}