2012-05-22 84 views
0

我需要將子字符串轉換爲NSString中的第二個逗號。子字符到第n個字符

輸入:

NSString *input = @"title, price, Camry, $19798, active"; 

所需的輸出:

NSString *output = @"title, price"; 

謝謝!

UPDATE:

我有以下,但問題是它需要跳過最後一個逗號:

NSString *output = [input rangeOfString:@"," options:NSBackwardsSearch]; 
+0

您可以對 - [NSString rangeOfString:]:進行嵌套調用,然後查找第二個逗號的索引,然後對整個輸入進行子串處理。 – mservidio

回答

3

這段代碼應該做你需要的東西:

NSString *input = @"title, price, Camry, $19798, active"; 

NSArray *array = [input componentsSeparatedByString:@","]; 

NSArray *subArray = [array subarrayWithRange:NSMakeRange(0, 2)]; 

NSString *output = [subArray componentsJoinedByString:@","]; 

NSLog(output); 
0

你可以嘗試這樣的事情:

NSArray *items = [list componentsSeparatedByString:@", "]; 
NSString result = @""; 
result = [result stringByAppendingString:[items objectAtIndex:0]]; 
result = [result stringByAppendingString:@", "]; 
result = [result stringByAppendingString:[items objectAtIndex:1]]; 

你必須如果你想避免異常,檢查你是否至少有兩件物品。

3

試試這個:

- (NSString *)substringOfString:(NSString *)base untilNthOccurrence:(NSInteger)n ofString:(NSString *)delim 
{ 
    NSScanner *scanner = [NSScanner scannerWithString:base]; 
    NSInteger i; 
    for (i = 0; i < n; i++) 
    { 
     [scanner scanUpToString:delim intoString:NULL]; 
     [scanner scanString:delim intoString:NULL]; 
    } 
    return [base substringToIndex:scanner.scanLocation - delim.length]; 
} 
1

你可以分裂 - >接頭 - >加入該字符串像這樣在objc:

NSString *input = @"title, price, Camry, $19798, active"; 
// split by ", " 
NSArray *elements = [input componentsSeparatedByString: @", "]; 
// grab the subarray 
NSArray *subelements = [elements subarrayWithRange: NSMakeRange(0, 2)]; 
// concat by ", " again 
NSString *output = [subelements componentsJoinedByString:@", "]; 
0

有真的沒有錯,只是寫代碼來執行你想要什麼。例如:

int commaCount = 0; 
int i; 
for (i = 0; i < input.count; i++) { 
    if ([input characterAtIndex:i] == (unichar) ',') { 
     commaCount++; 
     if (commaCount == 2) break; 
    } 
} 

NSString output = nil; 
if (commaCount == 2) { 
    output = [input substringToIndex:i]; 
} 
0

您可以創建一個NSString分類處理髮現的任何字符串的第n個出現。這是ARC的例子。

//NSString+MyExtension.h 

@interface NSString(MyExtension) 
-(NSString*)substringToNthOccurrence:(NSUInteger)nth 
          ofString:(NSString*)string; 
-(NSString*)substringToNthOccurrence:(NSUInteger)nth 
          ofString:(NSString*)string 
          options:(NSStringCompareOptions)options; 
@end 
@implementation NSString(MyExtension) 
-(NSString*)substringToNthOccurrence:(NSUInteger)nth 
          ofString:(NSString*)string 
{ 
    return [self substringToNthOccurrence:nth ofString:string options:0]; 
} 
-(NSString*)substringToNthOccurrence:(NSUInteger)nth 
          ofString:(NSString*)string 
          options:(NSStringCompareOptions)options 
{ 
    NSUInteger location = 0, 
     strlength = [string length], 
     mylength = [self length]; 

    NSRange range = NSMakeRange(location, mylength); 

    while(nth--) 
    { 
     location = [self rangeOfString:string 
           options:options 
           range:range].location; 
     if(location == NSNotFound || (location + strlength) > mylength) 
     { 
      return [self copy]; //nth occurrence not found 
     }   
     if(nth == 0) strlength = 0; //This prevents the last occurence from being included 

     range = NSMakeRange(location + strlength, mylength - strlength - location); 
    }   
    return [self substringToIndex:location]; 
} 
@end 

//main.m 
#import "NSString+MyExtension.h" 
int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     NSString *output = [@"title, price, Camry, $19798, active" substringToNthOccurrence:2 ofString:@","]; 
     NSLog(@"%@", output); 
    } 
} 

* 我會離開它作爲一個練習的人來實現可變版本。

相關問題