2016-03-09 23 views
1

我試圖在NSString中獲得重複的字符數。從NSString獲取普通字符作爲子串

例如,setupsetpreset作爲輸入,並返回「設置」與計數一起3

讓我用另一個例子來解釋。

輸入1: upsetpreset

預期輸出1:設置2

輸入2: qwertygdgpopgdg

預期輸出2: GDG 2

+1

子串的最小長度是多少? – trojanfoe

+0

顯然是2並使用它,我需要得到相同短語的出現 –

+1

假設下面的字符串。 Sheraheshehehesheshe 應該輸出什麼?正如你可以看到「她」和「他」一樣重複。 –

回答

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

+0

謝謝......但這個詞不應該與輸入一起提及。例如,輸入是setupsetpreset,輸出返回一個短語'set' –

+0

,你告訴你只需要計數。它會將你的計數返還給你。 –

+0

我提到了一個計數返回的輸出 –

1

後獲得大量的定製邏輯的一個非空字符串得到共性,我得到了解決: -

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爲您的查詢所需的邏輯。

+0

爲什麼使用字符串存儲數字? – trojanfoe

+0

我在數組內存儲數字以獲得NSArray的相同索引,該索引被視爲匹配次數最多的子串。 –

+0

爲什麼不使用'NSNumber'? – trojanfoe