Skip to content

Instantly share code, notes, and snippets.

@aorcsik
Last active September 27, 2017 16:22
Show Gist options
  • Select an option

  • Save aorcsik/c8210a84f163b1b644c0 to your computer and use it in GitHub Desktop.

Select an option

Save aorcsik/c8210a84f163b1b644c0 to your computer and use it in GitHub Desktop.

Revisions

  1. aorcsik revised this gist Nov 7, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion string-truncate.swift
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,7 @@ extension String {
    return self
    }
    }
    }
    }

    // Example
    let str = "This is a long string".truncate(10, trailing: "...") // "This is a ..."
  2. aorcsik created this gist Nov 7, 2014.
    11 changes: 11 additions & 0 deletions string-truncate.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    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 countElements(self) > length {
    return self.substringToIndex(advance(self.startIndex, length)) + (trailing ?? "")
    } else {
    return self
    }
    }
    }