Sunday, October 4, 2015

NSString substringToIndex - Truncate a string and add some characters

This is how to get the first n characters in a string and add extra "..." string to the output string:

import UIKit

let str = "Hello World This is a long string 1234567890."

var strTemp = str as NSString

//Keep the first 30 characters.
strTemp = strTemp.substringToIndex(30)

strTemp = (strTemp as String) + "..."


println(strTemp)

The result:

Hello World This is a long str...

1 comment: