我試圖在NSString中獲得重複的字符數。從NSString獲取普通字符作爲子串
例如,setupsetpreset作爲輸入,並返回「設置」與計數一起3
讓我用另一個例子來解釋。
輸入1: upsetpreset
預期輸出1:設置2
輸入2: qwertygdgpopgdg
預期輸出2: GDG 2
我試圖在NSString中獲得重複的字符數。從NSString獲取普通字符作爲子串
例如,setupsetpreset作爲輸入,並返回「設置」與計數一起3
讓我用另一個例子來解釋。
輸入1: upsetpreset
預期輸出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 「是」 - 2 「一個」 - 2 「測試」 - 2 「只有」 - 1
謝謝......但這個詞不應該與輸入一起提及。例如,輸入是setupsetpreset,輸出返回一個短語'set' –
,你告訴你只需要計數。它會將你的計數返還給你。 –
我提到了一個計數返回的輸出 –
後獲得大量的定製邏輯的一個非空字符串得到共性,我得到了解決: -
NSString *[email protected]"setupsetpreset";
[email protected]"sheraheshehehesheshe";
NSMutableArray *a=[[NSMutableArray alloc] init];
NSMutableArray *b=[[NSMutableArray alloc] init];
for (int i=0; i<(string.length); i++) {
[a addObject:[NSString stringWithFormat:@"%c",[string characterAtIndex:i]]];
}
NSCountedSet *countedSet = [NSCountedSet new];
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
[countedSet addObject:substring];
}];
NSMutableArray *c=[[NSMutableArray alloc] init];
for (int i=0; i<(string.length); i++) {
if([countedSet countForObject:[a objectAtIndex:i]]>=2){
if(![b containsObject:[a objectAtIndex:i]]){
[c addObject:[NSString stringWithFormat:@"%d",(int)[countedSet countForObject:[a objectAtIndex:i]]]];
}
[b addObject:[a objectAtIndex:i]];
}
}
int total=0;
for (NSString *s in c){
int m=[s intValue];
total+=m;
}
total=total/c.count;
NSString *s=[b componentsJoinedByString:@""];
[c removeAllObjects];
for (int i=0; i<(s.length); i++) {
if([countedSet countForObject:[b objectAtIndex:i]]>=total){
[c addObject:[b objectAtIndex:i]];
}
}
s=[c componentsJoinedByString:@""];
int iterate=0;
NSString *substring;
NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
while (iterate<s.length-total) {
NSString *searchstring=[s substringWithRange:NSMakeRange(iterate, total)];
if ([string rangeOfString:searchstring].location == NSNotFound) {
} else {
[dict setObject:[NSNumber numberWithInt:[[dict objectForKey:searchstring] intValue]+1] forKey:searchstring];
}
iterate++;
}
[a removeAllObjects];
[b removeAllObjects];
a=[dict.allValues mutableCopy];
b=[dict.allKeys mutableCopy];
int max = [[a valueForKeyPath:@"@max.intValue"] intValue];
NSInteger Aindex = [a indexOfObject:[NSNumber numberWithInt:max]];
substring =[NSString stringWithFormat:@"%@",[b objectAtIndex:Aindex]];
NSLog(@"Substring %@ is repeated %d times",substring,max);
希望它可以幫助FO r某人的需要:)感謝Kiran爲我提供了一套作爲零件和Ruchira爲您的查詢所需的邏輯。
子串的最小長度是多少? – trojanfoe
顯然是2並使用它,我需要得到相同短語的出現 –
假設下面的字符串。 Sheraheshehehesheshe 應該輸出什麼?正如你可以看到「她」和「他」一樣重複。 –