Shorten an NSString?

£可爱£侵袭症+ 提交于 2019-12-10 11:27:49

问题


I have a very simple question. Is there a built in method to shorten strings? If not can someone provide an example of doing that in ObjC?

For example:

ThisIsAVeryLongString

should become

ThisIsAV...

It needs to check if the string is over a certain amount of characters and if it is shorten it.


回答1:


It's pretty straightforward...

NSString *originalString = @"SomethingVeryLong";
int newLength = 9;
if (originalString.length > newLength)
    NSString *shortString = [originalString substringToIndex:newLength];


来源:https://stackoverflow.com/questions/3303101/shorten-an-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!