如何在Objective C中修剪两个可变字符串?

时间:2016-08-02 07:20:05

标签: ios objective-c xib

我正面临修剪NSMutableString的问题。具体来说,我找不到有关修剪两个可变字符串的信息。我正在做这样的事情。

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

    if(!myMutableString1Obj)
        myMutableString1Obj = [[NSMutableString alloc] initWithString:string];
    else
    {
        [myMutableString1Obj appendString:string];

        NSString *trimmedString1 = [myMutableString1Obj stringByTrimmingCharactersInSet:[NSCharacterSet symbolCharacterSet]];
        [myMutableString1Obj setString:trimmedString1];

        NSData *data1 = [myMutableString1Obj dataUsingEncoding:NSUTF8StringEncoding];
        response1dict = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
        NSLog(@"JSON DATA = %@",response1dict);


    }

    if(!myMutableStringObj)
        myMutableStringObj = [[NSMutableString alloc] initWithString:string];
    else
    {
        [myMutableStringObj appendString:string];

        NSString *trimmedString = [myMutableStringObj stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
        [myMutableStringObj setString:trimmedString];

        NSData *datasite = [myMutableStringObj dataUsingEncoding:NSUTF8StringEncoding];
        responsedict = [NSJSONSerialization JSONObjectWithData:datasite options:0 error:nil];
        NSLog(@"JSON DATA = %@",responsedict);


    }


        myMutableString2Obj=[[NSMutableString alloc]initWithString:string];
        NSLog(@"Array String: %@",myMutableString2Obj);
        NSData *data = [myMutableString2Obj dataUsingEncoding:NSUTF8StringEncoding];
        response2dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"JSON DATA = %@",response2dict);
}

这里我使用的是XML数据。

1 个答案:

答案 0 :(得分:0)

您能否请尝试以下代码:

[myMutableString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]

这将修剪你的字符串并删除新的行和空格