获取 NSString 中字符的索引,并带有偏移量 &在 Objective-C 中使用子字符串
我有一根绳子!
NSString *myString=[NSString stringWithFormat:@"This is my lovely string"];
我想要做的是:
- 假设字符串中的第一个字符位于索引 0 处。转到第 11 个字符(即上例中的“l”),并向后找到第一个出现的空格的位置(在上面的字符串中) ,如果我们从“l”向后移动,第一个出现的空间的位置是位置 10)。我们将此空间的索引称为“leSpace”,值为 10。
使用 ... 将剩余字符串作为新字符串的子串
[myString substringFromIndex:leSpace]
……我希望我已经解释得很好了。请帮忙,你能写一个片段或其他东西来帮助我完成这项任务吗?
I have a string!
NSString *myString=[NSString stringWithFormat:@"This is my lovely string"];
What I want to do is:
- Assuming the first character in the string is at index 0. Go to the 11th character (That is 'l' in the above case), and find the position of first occurring space backwards (In the above string, the position of first occurring space if we go backwards from 'l' is at position 10). Let's call the index of this space 'leSpace' having value 10.
Substring the remaining string to a new string using ...
[myString substringFromIndex:leSpace]
...I hope I have explained well. Please help, can you write a snippet or something to help me do this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于选项,请使用:
NSBackwardsSearch
示例:
NSLog 输出:
当然应该进行错误检查,以确保
range.location
不等于NSNotFound
For the options use:
NSBackwardsSearch
Example:
NSLog output:
Of course there should be error checking that
range.location
does not equalNSNotFound