Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate the string into required length in iphone sdk?

Tags:

objective-c

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

like image 294
monish Avatar asked May 26 '10 11:05

monish


People also ask

How do you truncate a string?

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.

How do you truncate a string in Swift?

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".

How do you truncate a long string in Python?

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.

What does it mean to truncate a string?

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.


2 Answers

str = [str substringToIndex: MIN(15, [str length])];
like image 79
tc. Avatar answered Sep 22 '22 06:09

tc.


Or,

str = [str substringToIndex: MIN(15, [str length])];

Now you are safely truncating to 15 or less.

like image 26
tgunr Avatar answered Sep 21 '22 06:09

tgunr