0

我有个问题。我的字符串有 format "abc",它有""。现在我只想abc"abc"(意思是从字符串中删除“”并只得到 abc)。我怎样才能做到这一点?提前致谢

4

6 回答 6

2
NSString *abc = [@"\"abc\"" stringByReplacingOccurencesOfString:@"\"" withString:@""];
于 2013-10-07T10:40:05.033 回答
1
#import <Foundation/Foundation.h>

int main(int argc, const char **argv) {
    @autoreleasepool {
        NSString *input = @"\"abc\"";
        NSArray *elements = [input componentsSeparatedByString:@"\""];
        if ([elements count] > 0) {
            NSString *element = [elements objectAtIndex:1];
            NSLog(@"count=%lu element='%@'", [elements count], element);
        }
    }
    return 0;
}

$ clang -o strsep strsep.m -framework Foundation
$ ./strsep
2013-10-07 11:42:44.274 strsep[34292:707] count=3 element='abc'
于 2013-10-07T10:38:08.180 回答
1

您可以将出现的 " 替换为空格。

NSString *some = @"\"abc\"";
NSLog(@"%@",some);
NSString *newString = [some stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSLog(@"%@",newString);
于 2013-10-07T10:43:09.210 回答
1
[String replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [String length])];
于 2013-10-07T10:39:43.253 回答
1

有一个内置函数使您能够用另一个子字符串替换所有出现的子字符串 - 如果另一个设置为空字符串 ( @"" ),它将有效地删除所有出现的子字符串。你的例子:

NSString * yourNewString = [yourOldString stringByReplacingOccurrencesOfString:@"\"" withString:@""]);
于 2013-10-07T10:42:15.217 回答
0

我为你写了一个方法,这将完美地工作:(如果你的最后一个字符是 " )

- (NSString*) getTheString:(NSString*) yourString{
    NSString* answerString = [[NSString alloc]init];

    int substrinFrom = 0;
    for (NSInteger charIdx=0; charIdx<[yourString length]; charIdx++){

        if ( '"' == [yourString characterAtIndex:charIdx]) {
            substrinFrom = charIdx;
            NSLog(@"%i",substrinFrom);
            break;
        }
    }
    answerString = [yourString substringWithRange:NSMakeRange(substrinFrom+1, [yourString length] - substrinFrom -2 )];

    return answerString;

}

测试它:

NSString* test = [self getTheString:@"\"Hello this is a test string, and it will work\""];
NSLog(@"%@",test);
于 2013-10-07T10:52:39.320 回答