Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
I have a text coming from the server in following way:
AT&T

How to remove & from the this string.

Additionally I dont want to replace on & with &

I am looking for a solution in which all the &xxx; will be changed in correct character.

I have tried stringByReplacingPercentEscapesUsingEncoding but it was not working.
 

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
What do you want it replaced with? Nothing?


I'm confused. What do you mean by "correct character"? And do you want that for & as well, or is it a special case?

Some of the examples:

" should change with "
& should change with &
'
< should change with <
>
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I don't think there's anything built-in to easily do this, so I've been using Google's GTMNSString+HTML category. I haven't been able to find a link to it at the "Google Toolbox for Mac" site so I extracted it from this github project: MWFeedParser
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I have a text coming from the server in following way:
AT&T

How to remove & from the this string.

Additionally I dont want to replace on & with &

I am looking for a solution in which all the &xxx; will be changed in correct character.

I have tried stringByReplacingPercentEscapesUsingEncoding but it was not working.

Some of the examples:

" should change with "
& should change with &
&apos;
< should change with <
>

It might be possible to do this using a UIWebView. I can't see that being a high performance solution though (create off-screen web view, insert string, let it render, read text back)
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
I ran into a similar problem on the project I am working on. I ended up implementing a category which can take care of the conversion for me using libxml2. First, I run through the string and convert any named entities into their hex equivalent, then I run the converted string through libxml2 to convert the hex entities into the unicode character. You can take a look at the code on GitHub. Find the NSString category named MITAdditions_HTMLEntity; - (NSString *)stringByDecodingXMLEntities is the method that performs the conversion. A word to the wise, though, the method is rarely used so it was a brute force approach and hasn't been optimized (or really cleaned up at all...).
 

blueillusion

macrumors member
Aug 18, 2008
56
3
take a look at the NSRegularExpression class, specifically the methods:

Code:
replaceMatchesInString:options:range:withTemplate:
stringByReplacingMatchesInString:options:range:withTemplate:

I've used this to go the other way around. ie < becomes <
You could easily use the functions to do the reverse.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.