如何从iphone中的字符串中删除引号和斜杠

时间:2011-09-13 11:17:47

标签: iphone objective-c string

嗨朋友们...我正在使用正则表达式,所以我得到字符串,但有双引号和斜线,但我不想要那个。我想要字符串值没有斜杠和双引号。我试着这个,但没有得到正确的答案。

运行应用[/Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/RegexKitLiteDemoAppDelegate.m:108:0 /Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/RegexKitLiteDemoAppDelegate.m:108: error: incompatible block pointer types initializing 'void (^)(struct NSString *, NSUInteger, BOOL *)', expected 'void (^)(struct objc_object *, NSUInteger, BOOL *)后出错 我得到了这个错误行

第二个是:[/Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/RegexKitLiteDemoAppDelegate.m:105:0 /Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/RegexKitLiteDemoAppDelegate.m:105: warning: 'NSString' may not respond to '+stringByTrimmingCharactersInSet:

我收到此错误行[webData length] encoding:NSUTF8StringEncoding];

    //NSLog(@"%@",loginStatus);  


    [connection release];
    //
    NSString *regexString = @"Stations\\[""(.*)""\\] = new Station\\((.*)new Array\\((.*)\\)\\);";  //@"Stations\\[""(.*)""\\] = new Station\\((.*)\\);"; //@"Stations\[""(.*)""\] = new Station\({[\,,2}(.*)new Array\((.*)\)\);";    //@"<a href=([^>]*)>([^>]*) - ";
    matchArray = [loginStatus arrayOfCaptureComponentsMatchedByRegex:regexString];
    NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:[matchArray count]];
    //NSCharacterSet *charactersToRemove = [NSCharacterSet punctuationCharacterSet];
    [matchArray enumerateObjectsUsingBlock:^(NSString *aString, NSUInteger idx, BOOL *stop) 
    {
        NSString *newString = [NSString stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];//#############
        [newArray insertObject:newString atIndex:idx];
        NSLog(@"matchArray: %@", matchArray);
    }];//******************


    //NSLog(@"matchArray: %@", matchArray);


    lstAirports = [[NSMutableArray alloc] initWithCapacity:[matchArray count]];

    for (int i = 0; i < [matchArray count]; i++) {
        airport *air=[[airport alloc]init];

        //code
        air.Code = [[matchArray objectAtIndex: i] objectAtIndex: 1];
        NSLog(@"air.Code: %@\n",air.Code);
        //name
        NSString *temp=[[matchArray objectAtIndex: i] objectAtIndex: 2];
        NSArray *arrParts=[temp componentsSeparatedByString:@""","];
        //air.Name=arrParts[2];
        air.Name=[arrParts objectAtIndex:2];
        NSLog(@"air.Name: %@\n",air.Name);
        //destination airports
        temp=[[matchArray objectAtIndex: i] objectAtIndex: 3];
        arrParts=[temp componentsSeparatedByString:@","];
        air.DestinationAirports =arrParts;
        NSLog(@"air.DestinationAirports: %@\n",air.DestinationAirports);
        [lstAirports addObject: air];
        NSLog(@"lstAirports: %@\n",lstAirports);
    }

    //[webData release]; 
} 
请一些人帮助我,这对我来说至关重要

2 个答案:

答案 0 :(得分:4)

您不需要RegExp来删除NSString中出现的字符串。

请参阅下面的示例,我可以帮助您:

NSString *str = @"fdf\"fdsfdsf\"fsdfsf/fsdfsdfsf\\fsdfsdf\\fsdffsd//fsdfsf\"fsdf/\\\"";
str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"\\" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"/" withString:@""];
NSLog(@"%@", str);

答案 1 :(得分:0)

检查

的用法

stringByReplacingOccurrencesOfString:withString

in

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

您可以用“”字符替换它们。