¡@

Home 

2014/10/15 ¤U¤È 10:14:32

iphone Programming Glossary: stringrange

How can I truncate an NSString to a set length?

http://stackoverflow.com/questions/2952298/how-can-i-truncate-an-nsstring-to-a-set-length

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