读取 NSString 中的每个字符

发布于 2024-11-02 12:09:18 字数 106 浏览 2 评论 0原文

是否可以在 Objective-C 框架内识别 NSString 中特定字符的位置和存在?例如,如果我有 NSString @"hello" 并且我想知道字符“e”的位置和存在,我该如何做到这一点?

Is it possible to identify the location and presence of a specific char in a NSString within the objective-c framework? For example, if I have the NSString @"hello" and I wanted to know the location and presence of the char "e", how would I be able to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

再浓的妆也掩不了殇 2024-11-09 12:09:18

有一种特殊的方法可以在字符串中搜索单个字符,但您可以只搜索长度为 1 的子字符串的范围并请求返回的范围的位置,如下所示:

NSRange charRange = [@"hello" rangeOfString:@"e"];
NSUInteger index = charRange.location;
if (index == NSNotFound) {
    NSLog(@"substring not found");
}

您可以在此处找到完整的文档:rangeOfString:

查找所有@"e"的索引@"hello" 不过,您可能想做这样的事情:

NSString *haystack = @"hellol";
NSString *needle = @"l";
NSMutableIndexSet *indices = [NSMutableIndexSet indexSet];
NSUInteger haystackLength = [haystack length];
NSRange range = NSMakeRange(0, haystackLength);
NSRange searchRange = range;
while (range.location != NSNotFound) {
    range = [haystack rangeOfString:needle options:0 range:searchRange];
    if (range.location != NSNotFound) {
        [indices addIndex:range.location];
        NSUInteger searchLocation = range.location + 1;
        NSUInteger searchLength = haystackLength - searchLocation;
        if (searchLocation >= haystackLength) {
            break;
        }
        searchRange = NSMakeRange(searchLocation, searchLength);
    }
}
//indices now holds the indices of all occurrences of 'e' in "hello".

文档:NSMutableIndexSetNSIndexSet

编辑: 将算法替换为@bbum 正如他对此答案的评论中所述。

There is particular method for searching for single characters within a string, but you can just search for the range of a substring of length 1 and request the returned range's location, as such:

NSRange charRange = [@"hello" rangeOfString:@"e"];
NSUInteger index = charRange.location;
if (index == NSNotFound) {
    NSLog(@"substring not found");
}

You can find full documentation here: rangeOfString:

To find the indices of all @"e" in @"hello" you might want to do something like this, though:

NSString *haystack = @"hellol";
NSString *needle = @"l";
NSMutableIndexSet *indices = [NSMutableIndexSet indexSet];
NSUInteger haystackLength = [haystack length];
NSRange range = NSMakeRange(0, haystackLength);
NSRange searchRange = range;
while (range.location != NSNotFound) {
    range = [haystack rangeOfString:needle options:0 range:searchRange];
    if (range.location != NSNotFound) {
        [indices addIndex:range.location];
        NSUInteger searchLocation = range.location + 1;
        NSUInteger searchLength = haystackLength - searchLocation;
        if (searchLocation >= haystackLength) {
            break;
        }
        searchRange = NSMakeRange(searchLocation, searchLength);
    }
}
//indices now holds the indices of all occurrences of 'e' in "hello".

Documentation: NSMutableIndexSet, NSIndexSet

Edit: Replaced algorithm with the one from @bbum's as described in his comment to this answer.

滥情稳全场 2024-11-09 12:09:18

查看 Apple 文档

- (NSRange)rangeOfString:(NSString *)aString

Take a look at the Apple Docs for NSString.

- (NSRange)rangeOfString:(NSString *)aString
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文