Objective-C 中的 NSRange 有问题吗?
我对 NSRange 有一点问题,或者可能只是我使用了错误的命令。
这就是我想做的。我有一个像这样的字符串:
NSString *mystring = @"/c1blue/c2green/c3yellow/"
如您所见,总是有一个带有值的命令,并且用“/”分隔。现在我想编写一个方法,为我提供一个命令的特定值,例如 c2 ,它会是绿色的。
首先我会得到 c2 的位置:
int beginIndex = [mystring rangeOfString:@"c2"].location;
现在我需要找到“/”的位置以及'beginIndex'的偏移量。
这就是我不知道如何做的地方。
非常感谢您的帮助。 谢谢
I have a little problem with NSRange, or maybe its just the wrong comman I use.
Here is what I want to do. I have a string like this :
NSString *mystring = @"/c1blue/c2green/c3yellow/"
As you can see there is always a command with a value and that seperated by "/". Now I want to write a mthod that gives me a specific value for a command, e.g. c2 which would be green.
First I would get the position of c2 :
int beginIndex = [mystring rangeOfString:@"c2"].location;
Now I need to find the position of the "/" with the offset of 'beginIndex'.
And thats where I do not know how.
Help is hihly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先使用
NSString
的componentsSeparatedByString:
方法将字符串拆分为数组怎么样?How about splitting the string into array first, using the
componentsSeparatedByString:
method ofNSString
?怎么样:
结果:
How about this:
Result:
另一种方法:
测试代码:
结果:
Another approach:
Test code:
Result:
看看 NSString 参考,我认为你需要的是:
- (NSArray *)componentsSeparatedByString:(NSString *)separator
Look at NSString reference and i think what you need is:
- (NSArray *)componentsSeparatedByString:(NSString *)separator
谢谢,
我实际上想出了这个,但它看起来比建议的更复杂:
Thanks,
I actually came up with this one, but it looks more complicated than the suggested ones :