I am having a string which contains more the 25 characters;
NSString *str = @"This is the string to be truncated to 15 characters only";
In the above string I need only the 15 characters to be stored in another variable after truncation.
can anyone suggest me how to do this? Anyone's help will be much appreciated.
Thank you, Monish
If the length of the string is less than or equal to the given number, just return the string without truncating it. Otherwise, truncate the string. Meaning keep the beginning of the string up until the given number and discard the rest. Add “…” to the end of the truncated string and return it.
string-truncate.swift Truncates the string to the specified length number of characters and appends an optional trailing string if longer. - Parameter trailing: A 'String' that will be appended after the truncation. - Returns: 'String' object. let str = "I might be just a little bit too long".
The other way to truncate a string is to use a rsplit() python function. rsplit() function takes the string, a delimiter value to split the string into parts, and it returns a list of words contained in the string split by the provided delimiter.
Truncation in IT refers to “cutting” something, or removing parts of it to make it shorter. In general, truncation takes a certain object such as a number or text string and reduces it in some way, which makes it less resources to store.
str = [str substringToIndex: MIN(15, [str length])];
Or,
str = [str substringToIndex: MIN(15, [str length])];
Now you are safely truncating to 15 or less.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With