Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在:之后和之前拆分字符串?_Ios_Objective C_Nsstring - Fatal编程技术网

Ios 在:之后和之前拆分字符串?

Ios 在:之后和之前拆分字符串?,ios,objective-c,nsstring,Ios,Objective C,Nsstring,我有一根像这样的长绳子: je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent? 我想在……之后和之前分开绳子。因此,我想将其放入数组中: {e,es,e,ons,ez,en} 我知道我可以做到这一点: NSArray *subStrings = [stringToChange componentsSeparatedByString:@":"]; 但这还不够。 请问我该怎么做?任何帮助都将

我有一根像这样的长绳子:

je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?
我想在……之后和之前分开绳子。因此,我想将其放入数组中:

{e,es,e,ons,ez,en}
我知道我可以做到这一点:

 NSArray *subStrings = [stringToChange componentsSeparatedByString:@":"];
但这还不够。
请问我该怎么做?任何帮助都将不胜感激

使用
NSScanner
解析字符串,搜索
标记。您可以丢弃不需要的文本,捕获所需的部分(基于找到的标记),并将它们添加到数组中。

试试这个

  NSString *names = @"je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?";
NSArray *namesarray = [names componentsSeparatedByString:@":"];
NSMutableArray *desiredArray = [[NSMutableArray alloc] initWithCapacity:0];
[namesarray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSRange rangeofsign = [(NSString*)obj rangeOfString:@"?"];
NSString *extractedName = [(NSString*)obj substringToIndex:rangeofsign.location];
[desiredArray addObject:extractedName];
 }];
NSLog(@"%@",desiredArray);

为了更简单,您可以尝试以下代码:

NSString *string = @"je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?";
NSArray *firstArray = [string componentsSeparatedByString:@":"];
NSMutableArray *finalArray = [[NSMutableArray alloc] init];
for (NSString *newString in firstArray) {
    NSArray *tempArray = [newString componentsSeparatedByString:@"?"];
    if ([tempArray count] > 1) {
        [finalArray addObject:[tempArray firstObject]];
    }
}
NSLog(@"%@",finalArray);

另一种选择是使用类

使用此模式:

\\:([^\?]+)\?
示例代码:

NSString *sample  = @"je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?";
NSString *pattern = @"\\:([^\?]+)\?";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];


NSArray *matches = [regex matchesInString:sample
                                  options:0
                                    range:NSMakeRange(0, [sample length])];
if(!error && [matches count] > 0) {
    for (NSTextCheckingResult *match in matches) {
        NSRange matchRange = [match range];
        NSLog(@"%@" ,[sample substringWithRange:matchRange]);
    }
}
输出:

2015-07-03 12:16:39.037 TestRegex[3479:48301] :e
2015-07-03 12:16:39.039 TestRegex[3479:48301] :es
2015-07-03 12:16:39.039 TestRegex[3479:48301] :e
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ons
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ez
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ent

ii由于未捕获异常“NSRangeException”,在arrayTerminating应用程序中获取相同的字符串,原因:'***-[\uu NSCFString substring to Index:]:索引9223372036854775807超出范围;字符串长度8'您需要一些变通代码,它不是经过测试的代码。