0

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

输入:

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

期望的输出:

NSString *output = @"title, price";

谢谢!

更新:

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

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

6 回答 6

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];
}
于 2012-05-22T19:43:43.957 回答
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);
于 2012-05-22T19:51:36.337 回答
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:@", "];
于 2012-05-22T19:48:48.020 回答
0

你可以尝试这样的事情:

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

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

于 2012-05-22T19:40:51.123 回答
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];
}
于 2012-05-22T19:49:54.860 回答
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);
    }
}

*我将把它作为练习留给某人来实现可变版本。

于 2012-05-22T20:28:18.840 回答