Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I truncate an NSString to a set length?

I searched, but surprisingly couldn't find an answer.

I have a long NSString that I want to shorten. I want the maximum length to be around 20 characters. I read somewhere that the best solution is to use substringWithRange. Is this the best way to truncate a string?

NSRange stringRange = {0,20};
NSString *myString = @"This is a string, it's a very long string, it's a very long string indeed";
NSString *shortString = [myString substringWithRange:stringRange];

It seems a little delicate (crashes if the string is shorter than the maximum length). I'm also not sure if it's Unicode-safe. Is there a better way to do it? Does anyone have a nice category for this?

like image 537
nevan king Avatar asked Jun 01 '10 18:06

nevan king


6 Answers

Actually the part about being "Unicode safe" was dead on, as many characters combine in unicode which the suggested answers don't consider.

For example, if you want to type é. One way of doing it is by typing "e"(0x65)+combining accent" ́"(0x301). Now, if you type "café" like this and truncate 4 chars, you'll get "cafe". This might cause problems in some places.

If you don't care about this, other answers work fine. Otherwise, do this:

// define the range you're interested in
NSRange stringRange = {0, MIN([myString length], 20)};

// adjust the range to include dependent chars
stringRange = [myString rangeOfComposedCharacterSequencesForRange:stringRange];

// Now you can create the short string
NSString *shortString = [myString substringWithRange:stringRange];

Note that in this way your range might be longer than your initial range length. In the café example above, your range will expand to a length of 5, even though you still have 4 "glyphs". If you absolutely need to have a length less than what you indicated, you need to check for this.

like image 108
mohsenr Avatar answered Nov 13 '22 20:11

mohsenr


Swift 4

let trimToCharacter = 20
let shortString = String(myString.prefix(trimToCharacter))

Happy Coding.

like image 44
Md. Ibrahim Hassan Avatar answered Nov 13 '22 19:11

Md. Ibrahim Hassan


Since this answer isn't actually in this list, the simplest and most sensible one-liner:

NSString *myString = @"This is a string, it's a very long string, it's a very long string indeed";
myString = (myString.length > 20) ? [myString substringToIndex:20] : myString;
like image 15
brandonscript Avatar answered Nov 13 '22 20:11

brandonscript


A shorter solution is:

NSString *shortString = ([myString length]>MINLENGTH ? [myString substringToIndex:MINLENGTH] : myString);
like image 11
Phoenix Avatar answered Nov 13 '22 19:11

Phoenix


It seems a little delicate (crashes if the string is shorter than the maximum length)

Then why not fix that part of it?

NSRange stringRange = {0, MIN([myString length], 20)};
like image 7
Shaggy Frog Avatar answered Nov 13 '22 19:11

Shaggy Frog


Could use a ternary operation:

NSString *shortString = (stringRange.length <= [myString length]) ? myString : [myString substringWithRange:stringRange];

Or for more control over the end result:

if (stringRange.length > [myString length])
    // throw exception, ignore error, or set shortString to myString
else 
    shortString = [myString substringWithRange:stringRange];
like image 3
Alex Reynolds Avatar answered Nov 13 '22 19:11

Alex Reynolds