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

问题描述 投票:0回答:1

我正面临修剪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数据。

ios objective-c xib
1个回答
0
投票

你可以试试下面的代码:

[myMutableString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]

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

© www.soinside.com 2019 - 2024. All rights reserved.