简体   繁体   中英

How to remove new line characters from NSString in a webservice response?

I am presently getting a Webservice Response which contains many new line characters. I have tried the following approaches but still i am not able to eliminate the New Line Characters.

1)

responseString = [responseString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

2)

responseString = [responseString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

3)

NSRange foundRange = [responseString rangeOfString:@"\n"];
    if (foundRange.location != NSNotFound)
        [responseString stringByReplacingOccurrencesOfString:@"\n"
                                            withString:@""
                                               options:0 
                                                 range:foundRange];

My Webservice respsonse is in this format.

META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use."?      


    wddxPacket version='1.0'><header/><data><string>{"MESSAGE":"","CODE":1,"RESPONSE":{"FILENAME":"CustomerSkillsIntro","PLAYLIST":[{"TIMEOUT":73,"TITLE":"Greet","QUESTIONNUMBER":1,"TIMEIN":71,"VALIDRESPONSE":1},{"TIMEOUT":77,"TITLE":"Have Name Tag","QUESTIONNUMBER":2,"TIMEIN":74,"VALIDRESPONSE":1},{"TIMEOUT":83,"TITLE":"Greet","QUESTIONNUMBER":3,"TIMEIN":78,"VALIDRESPONSE":1},{"TIMEOUT":112,"TITLE":"Helping Do My Job","QUESTIONNUMBER":4,"TIMEIN":109,"VALIDRESPONSE":1},{"TIMEOUT":134,"TITLE":"Greet Happily","QUESTIONNUMBER":5,"TIMEIN":131,"VALIDRESPONSE":1},{"TIMEOUT":144,"TITLE":"Stay cheerful when resident is crabby","QUESTIONNUMBER":6,"TIMEIN":141,"VALIDRESPONSE":1},{"TIMEOUT":154,"TITLE":"Bond with the new resident","QUESTIONNUMBER":7,"TIMEIN":151,"VALIDRESPONSE":1},...................

My requirement is to capture only the part of the string from {"MESSAGE":"","CODE":1, till the end. But i am getting too many white spaces and new line characters before the required part.

It looks like you could simplify your problem by taking string from first occurance of '{' to last occurance of '}' .

Code below ensures the result you want with different approach. Why go trough the process of removing white space if you say you need only the part "from {"MESSAGE":"","CODE":1, till the end.`"

NSRange start = [responseString rangeOfString:@"{"];
NSRange end = [responseString rangeOfString:@"}" options:NSBackwardsSearch];
NSString *result = nil;

if ((start.location != NSNotFound)&&(start.location != NSNotFound))
{
    NSRange resultRange = NSMakeRange(start.location,end.location - start.location + 1);
    result = [responseString substringWithRange: resultRange];
    NSLog (@"returning with result: %@", result);
}
else 
{
    NSLog (@"abort mission");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM