the best way to "clean" a string?

Discussion in 'iOS Programming' started by patrover, Sep 7, 2009.

  1. patrover macrumors newbie

    Joined:
    Aug 15, 2009
    #1
    i have a string: (2434)-45455

    and i want to filter out all the non-numeric chars ( i.e "()-" ), what is the best way?


    thanks
     
  2. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    stringByTrimmingCharactersInSet: probably won't be useful for this purpose.

    Look at the string programming guide, as suggested. Also look at NSScanner. If you prefer regular expressions you could look into that as well.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Just curious: why do you say this?
     
  5. patrover thread starter macrumors newbie

    Joined:
    Aug 15, 2009
    #6
    if i'm not mistaken trimming is only for the edges of the string.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Ah, that's right. Perhaps one of the stringByReplacing... methods will suit your needs.
     
  7. RaceTripper macrumors 68030

    Joined:
    May 29, 2007
    #8
    Can you use regex? Use that to replace all [^\d] with the empty string.

    In perl it would be: s/[^\d]//g
     

Share This Page