gpt4 book ai didi

ios - 从 NSString 获取常用字符作为子字符串

转载 作者:行者123 更新时间:2023-12-01 18:45:54 24 4
gpt4 key购买 nike

我试图在 NSString 中获取重复的字符及其计数。

例如,setupsetpreset 作为输入,它返回 'set' 和计数 3。

让我用另一个例子来解释。

输入 1:
预设

预期输出 1:设置 2

输入 2:
qwertygdgpopgdg

预期输出 2: gdg 2

最佳答案

请参阅下文

NSString *string     = @"This is a test. This is only a test.";
NSCountedSet *countedSet = [NSCountedSet new];

[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByWords | NSStringEnumerationLocalized
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){

// This block is called once for each word in the string.
[countedSet addObject:substring];

// If you want to ignore case, so that "this" and "This"
// are counted the same, use this line instead to convert
// each word to lowercase first:
// [countedSet addObject:[substring lowercaseString]];
}];

NSLog(@"%@", countedSet);

结果:

“这个” - 2
"is" - 2
“a2
“测试” - 2
“只有1个

关于ios - 从 NSString 获取常用字符作为子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35886302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com