Skip to main content
remove quote formatting.
Source Link
Alex Brown
  • 43.1k
  • 10
  • 96
  • 109

Truncate String to Specific Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int) -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = str.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 

Truncate String to Specific Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int) -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = str.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 

Truncate String to Specific Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int) -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = str.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 
deleted 24 characters in body
Source Link
ViJay Avhad
  • 2.7k
  • 24
  • 26

swift3 update: TruncateTruncate String to SpecifiedSpecific Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int, trailing: String? = "") -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = str.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 

swift3 update: Truncate String to Specified Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int, trailing: String? = "") -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = str.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 

Truncate String to Specific Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int) -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = str.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 
deleted 1 character in body
Source Link
ViJay Avhad
  • 2.7k
  • 24
  • 26

swift3 update: Truncate String to Specified Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int, trailing: String? = "") -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = taskstr.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 

swift3 update: Truncate String to Specified Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int, trailing: String? = "") -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = task.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 

swift3 update: Truncate String to Specified Length

If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class

extension String { func trunc(_ length: Int, trailing: String? = "") -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

Use

var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = str.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 
deleted 1 character in body
Source Link
Forge
  • 6.9k
  • 7
  • 49
  • 65
Loading
swift3 code update
Source Link
ViJay Avhad
  • 2.7k
  • 24
  • 26
Loading
swift3 code update
Source Link
ViJay Avhad
  • 2.7k
  • 24
  • 26
Loading
Source Link
ViJay Avhad
  • 2.7k
  • 24
  • 26
Loading