NSString 或 NSMutableString 中字符的位置

发布于 2024-08-23 14:55:15 字数 451 浏览 8 评论 0原文

我已经搜索了几个小时,但还没有找到解决我的问题的方法。 如下所示:

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-08-30 14:55:15

要查找字符串中字符串的出现次数,请使用 NSString 类。然后,您可以构造 NSRanges 来提取子字符串等。

此示例仅删除示例字符串中的第一组开/闭括号...

NSString *original = @"\"spacer\": \[\"value1\", \"value2\"], \"spacer\": \[\"value1\", \"value2\"]";
NSLog(@"%@", original);

NSRange startRange = [original rangeOfString:@"\["];
NSRange endRange = [original rangeOfString:@"]"];

NSRange searchRange = NSMakeRange(0, endRange.location);
NSString *noBrackets = [original stringByReplacingOccurrencesOfString:@"\[" withString:@"" options:0 range:searchRange];
noBrackets = [noBrackets stringByReplacingOccurrencesOfString:@"]" withString:@"" options:0 range:searchRange];
NSLog(@"{%@}", noBrackets);

字符串编程指南 有更多详细信息。
您也可以使用 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...

NSString *original = @"\"spacer\": \[\"value1\", \"value2\"], \"spacer\": \[\"value1\", \"value2\"]";
NSLog(@"%@", original);

NSRange startRange = [original rangeOfString:@"\["];
NSRange endRange = [original rangeOfString:@"]"];

NSRange searchRange = NSMakeRange(0, endRange.location);
NSString *noBrackets = [original stringByReplacingOccurrencesOfString:@"\[" withString:@"" options:0 range:searchRange];
noBrackets = [noBrackets stringByReplacingOccurrencesOfString:@"]" withString:@"" options:0 range:searchRange];
NSLog(@"{%@}", noBrackets);

The String Programming Guide has more details.
You might alternatively also be able to use the NSScanner class.

堇色安年 2024-08-30 14:55:15

这有助于我将所有内容提取到字符的索引,在本例中为“[”:

NSString *original = @"\"spacer\": \[\"value1\", \"value2\"], \"spacer\": \[\"value1\", \"value2\"]";
NSRange range = [original rangeOfString:@"\["];
NSString *toBracket = [NSString stringWithString :[original substringToIndex:range.location] ];
NSLog(@"toBracket: %@", toBracket);

This worked for me to extract everything to the index of a character, in this case '[':

NSString *original = @"\"spacer\": \[\"value1\", \"value2\"], \"spacer\": \[\"value1\", \"value2\"]";
NSRange range = [original rangeOfString:@"\["];
NSString *toBracket = [NSString stringWithString :[original substringToIndex:range.location] ];
NSLog(@"toBracket: %@", toBracket);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文