monish
monish

Reputation: 1405

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

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

Upvotes: 15

Views: 14939

Answers (2)

tc.
tc.

Reputation: 33592

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

Upvotes: 41

tgunr
tgunr

Reputation: 1548

Or,

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

Now you are safely truncating to 15 or less.

Upvotes: 26

Related Questions