开发者

Remove characters from the beginning of an NSString

I have an NSString

NSString *data = @"abcdefghi";

but I want the data to be the "defghi"

What can 开发者_JS百科I do to change it?


You could do :

NSMutableString *data = [NSMutableString stringWithString:@"abcdefghi"];
[data deleteCharactersInRange:NSMakeRange(0, 3)];

This is using NSMutableString which allows you to delete and add Characters/Strings to itself.

The method used here is deleteCharactersInRange this deletes the letters in the NSRange, in this case the range has a location of 0, so that it starts at the start, and a length of 3, so it deletes 3 letters in.


[data substringFromIndex:2]

this will return a new String with the characters up to the index (2) clipped.


An alternative would be NSScanner if you don't know the exact location of your chars you want to delete: Apple Guide to NSScanners. You might want to look at the rest of the guide as well, as it very good describes what you can do with a string in Obj-C.


NSString is immutable. You need to use NSMutableString unless you want to create a new string. Look at deleteCharactersInRange method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜