子串到第n个字符

时间:2012-05-22 19:33:08

标签: objective-c

我需要子串到NSString中的第二个逗号。

输入:

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

期望的输出:

NSString *output = @"title, price";

谢谢!

更新:

我有以下但问题是它需要跳过最后一个逗号:

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

6 个答案:

答案 0 :(得分:4)

试试这个:

- (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 :(得分: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);

答案 2 :(得分:1)

您可以拆分 - >拼接 - &gt;在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:@", "];

答案 3 :(得分:0)

您可以尝试这样的事情:

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

如果你想避免例外,你必须检查你至少有两个项目。

答案 4 :(得分: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];
}

答案 5 :(得分: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);
    }
}

* 我会把它留作某人实施可变版本的练习。