how to remove & from NSString

Discussion in 'iOS Programming' started by Narendar Singh, Dec 11, 2012.

  1. Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    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.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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?
     
  3. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #3
    Some of the examples:

    " should change with "
    & should change with &
    '
    < should change with <
    >
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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)
     
  6. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #6
    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...).
     
  7. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
  8. blueillusion macrumors member

    Joined:
    Aug 18, 2008
    #8
    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.
     

Share This Page