如何从字符串中删除字符串

时间:2013-04-22 03:27:10

标签: objective-c xml nsstring

我有这个字符串

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"IsSuccess":true,"Result":"","Obj":
[{"FACULTY_CODE":1,"FACULTY_DESC":"\u0E1A\u0E23\u0E34\u0E2B\u0E32\u0E23\u0E18\u0E38\u0E23\u0
E01\u0E34\u0E08","FACULTY_DESC_ENG":"Faculty of Business 
Administration","FACULTY_CODE_ICL":"00039","FACULTY_GROUP":"1000"},
{"FACULTY_CODE":2,"FACULTY_DESC":"\u0E19\u0E34\u0E15\u0E34\u0E28\u0E32\u0E2A\u0E15\u0E23\u0E
4C","FACULTY_DESC_ENG":"Faculty of Law","FACULTY_CODE_ICL":"00037","FACULTY_GROUP":"0101"},
{"FACULTY_CODE":3,"FACULTY_DESC":"\u0E28\u0E34\u0E25\u0E1B\u0E28\u0E32\u0E2A\u0E15\u0E23\u0E
4C","FACULTY_DESC_ENG":"Faculty of Liberal 
Arts","FACULTY_CODE_ICL":"00096","FACULTY_GROUP":"0100"},
{"FACULTY_CODE":4,"FACULTY_DESC":"\u0E27\u0E34\u0E17\u0E22\u0E32\u0E28\u0E32\u0E2A\u0E15\u0E
23\u0E4C\u0E41\u0E25\u0E30\u0E40\u0E17\u0E04\u0E42\u0E19\u0E42\u0E25\u0E22\u0E35","FACULTY_D
ESC_ENG":"Faculty of Science and 
Technology","FACULTY_CODE_ICL":"00083","FACULTY_GROUP":"0303"},
{"FACULTY_CODE":5,"FACULTY_DESC":"\u0E23\u0E31\u0E10\u0E28\u0E32\u0E2A\u0E15\u0E23\u0E4C","F
ACULTY_DESC_ENG":"Faculty of Political 
Sciences","FACULTY_CODE_ICL":"00068","FACULTY_GROUP":"0101"},
{"FACULTY_CODE":7,"FACULTY_DESC":"\u0E19\u0E34\u0E40\u0E17\u0E28\u0E28\u0E32\u0E2A\u0E15\u0E
23\u0E4C","FACULTY_DESC_ENG":"Faculty of Communication 
Arts","FACULTY_CODE_ICL":"00038","FACULTY_GROUP":"0104"},
{"FACULTY_CODE":8,"FACULTY_DESC":"\u0E27\u0E34\u0E17\u0E22\u0E32\u0E25\u0E31\u0E22\u0E19\u0E
32\u0E19\u0E32\u0E0A\u0E32\u0E15\u0E34\u0E14\u0E34\u0E29\u0E22\u0E30\u0E28\u0E23\u0E34\u0E19
","FACULTY_DESC_ENG":"Didyasarin International 
College","FACULTY_CODE_ICL":"00137","FACULTY_GROUP":""},
{"FACULTY_CODE":9,"FACULTY_DESC":".","FACULTY_DESC_ENG":"Bachelor of Fine and Applied 
Art","FACULTY_CODE_ICL":"","FACULTY_GROUP":""}]}</string>

但我想删除"<?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">""</string>"

我正在使用此代码

NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"{.+}" options:0 error:&error];
    NSArray *matches = [regex matchesInString:dataString options:0 range:NSMakeRange(0, [dataString length])];
NSLog(@"matches = %@",matches );
for (NSTextCheckingResult *match in matches) {
    NSRange matchRange = [match range];
    newDataString = [dataString substringWithRange:matchRange];
}

它不起作用。

2 个答案:

答案 0 :(得分:0)

您可以像这样使用正则表达式:

%s/\"[^"]+\":\s*\" *\"\(\s*,\s*\)*//g

这是一个gvim正则表达式

它将摆脱每个键值对,其值为“”或“” 删除逗号,如果它在那里

答案 1 :(得分:0)

你可以做的只是

NSString *string1 = @"<?xml version=\"1.0\" encoding=\"utf-8\"?> <string xmlns=\"http://tempuri.org/\">";
NSString *string2 = @"</string>";
NSString *myString;//this is your string
myString = [myString stringByReplacingOccurrencesOfString:string1 withString:@""];
myString = [myString stringByReplacingOccurrencesOfString:string2 withString:@""];