【发布时间】:2013-03-18 06:42:51
【问题描述】:
这似乎是我正在寻找的,但反过来。我希望string 从右侧而不是从左侧提取。
给出从左边提取的例子:
NSString *source = @"0123456789";
NSString *firstFour = [source substringToIndex:4];
Output: "0123"
我正在寻找一个可以从右侧工作的下面的版本(下面的不工作)
NSString *source = @"0123456789";
NSString *lastFour = [source substringToIndex:-4];
Output: "6789"
[source substringFromIndex:6]; 不起作用,因为有时我会得到 000123456789 或 456789 或 6789 的答案。在所有情况下,我只需要字符串中的最后 4 个字符,以便将其转换为数字。
一定有比一堆 if else 语句更好的方法吗?
【问题讨论】:
-
[ source substringFromIndex:source.length - 4 ] -
与this question非常相似。
标签: ios objective-c string text-extraction