0

I am working on an iOS application. I am receiving json from a webservice and parse it into different NSStrings. The problem is sometimes the strings include special characters that have not been decoded.

For example I could get: Test One"Two as the NSString.

How do I remove special characters like this one from an NSString?

Stephen
  • 1,427
  • 1
  • 17
  • 42
  • Are those HTML escapes? – borrrden Apr 04 '13 at 03:02
  • 2
    possible duplicate of [Objective-C: How to replace HTML entities?](http://stackoverflow.com/questions/2364797/objective-c-how-to-replace-html-entities) and/or [HTML character decoding in Objective-C / Cocoa Touch](http://stackoverflow.com/questions/1105169/html-character-decoding-in-objective-c-cocoa-touch) – Michael Dautermann Apr 04 '13 at 03:03
  • I think this answer might solve your problem http://stackoverflow.com/a/2843454/778552 – Alex Marchant Apr 04 '13 at 03:09
  • Try this i hope this will help you to solve your issue.... NSString *book=@"Test One"Two"; book=[book stringByConvertingHTMLToPlainText]; https://www.dropbox.com/sh/99ihm91gl9j428k/X3DOjolAte – Arun Apr 04 '13 at 05:05

2 Answers2

0

I wrote this class awhile back to assist with stripping HTML from NSString.

NSString+StripHTML

NSString+StripHTML.h

#import <Foundation/Foundation.h>

@interface NSString (StripHTML)

-(NSString *)stringByStrippingHTML;

@end

NSString+StripHTML.m

#import "NSString+StripHTML.h"

@implementation NSString (StripHTML)

-(NSString *) stringByStrippingHTML {
    NSRange r;
    NSString *s = [self copy];
    while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    return s;
}

@end

Hope this helps.

random
  • 8,568
  • 12
  • 50
  • 85
  • this is not quite what I'm looking for. The string is not actually in HTML format. Just has the random codes in it. Also I don't want to remove it, just properly represent it – Stephen Apr 04 '13 at 03:25
0

I think you want just a simple Regex replacement:

This is what Neevik showed in Use regular expression to find/replace substring in NSString

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&[^;]*;" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
NSLog(@"%@", modifiedString);

That should remove your special characters.

Community
  • 1
  • 1
HalR
  • 11,411
  • 5
  • 48
  • 80
  • From his comment from the other answer: "I don't want to remove it, just properly represent it." Also, your regex is too hungry: @"Roger & Ebert think "The Shining!" was great.". I might suggest `@"?[a-z0-9]+;"` for the pattern and use case insensitive search. That gets you closer while still being pretty simple. – Rob Apr 04 '13 at 05:10