NSString 或 NSMutableString 中字符的位置
我已经搜索了几个小时,但还没有找到解决我的问题的方法。 如下所示:
"spacer": ["value1", "value2"], "spacer": ["value1", "value2"], ...
我有一个 NSString , 想要做的就是从字符串中删除 [ ] 字符。对于 Objective-c 来说这似乎是不可能的。大多数其他编程语言都提供诸如 strpos 或 indexOf 之类的函数,它们允许我搜索字符或字符串并找到它的位置。但在 Objective-c 中似乎没有这样的事情。
有谁知道如何删除这些字符? 此外,字符串中应该保留 [] 字符,因此我不能只使用 NSMutableString stringByReplacingOccurencesOfString:withString。我需要首先搜索间隔字符串,然后仅删除接下来的两个 [] 字符。
谢谢你帮助我。
I have searched for hours now and haven't found a solution for my problem. I have a NSString which looks like the following:
"spacer": ["value1", "value2"], "spacer": ["value1", "value2"], ...
What I want to do is to remove the [ ] characters from the string. It's seems to be impossible with objective-c. Most other programming languages offer functions like strpos or indexOf which allow me to search for a character or string and locate the position of it. But there seems nothing to be like this in objective-c.
Does anyone has an idea on how to remove these characters?
Additionally there are [] characters in the string which should remain, so I can't just use NSMutableString stringByReplacingOccurencesOfString:withString. I need to search first for the spacer string and then remove only the next two [] chars.
Thank you for helping me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要查找字符串中字符串的出现次数,请使用 NSString 类。然后,您可以构造 NSRanges 来提取子字符串等。
此示例仅删除示例字符串中的第一组开/闭括号...
字符串编程指南 有更多详细信息。
您也可以使用 NSScanner 类。
To find occurrences of a string within a string, use the rangeOfXXX methods in the NSString class. Then you can construct NSRanges to extract substrings, etc.
This example removes only the first set of open/close brackets in your sample string...
The String Programming Guide has more details.
You might alternatively also be able to use the NSScanner class.
这有助于我将所有内容提取到字符的索引,在本例中为“[”:
This worked for me to extract everything to the index of a character, in this case '[':