Checking a specific letter on NSString.

Discussion in 'iOS Programming' started by TalShani, Jun 30, 2010.

  1. TalShani macrumors member

    Joined:
    Jun 29, 2010
    #1
    Hello Guys..
    I have an NSString named word (as you can guess, its one word),
    I don't know the length of the string.
    I need to check if the last letter (and only the last),
    Contains the sign "?" or "!", or any other sign that is not A-Z, a-z or 1-9.
    If the sign is not A-Z, a-z, or 1-9,
    than the sign will be erased from the string.
    Exsample: hello! = hello.
    Please just help me find the command for doing that,
    I'm looking for it for a long time now..
    Hope you will answer me as soon as possible.
    Tal Shani.
    :apple:
     
  2. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #2
    You can use this method - (unichar)characterAtIndex:(NSUInteger)index in the NSString class to get the last character. Then compare if it is equal to any of the ones you are looking for. If it is, you can then get a substring with a range of 0 to the length minus one.
     
  3. TalShani thread starter macrumors member

    Joined:
    Jun 29, 2010
    #3
    And how I can get the full length of the string?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Start by learning to read the documentation. There's an instance method of NSString that gives you exactly what you want.
     
  5. TalShani thread starter macrumors member

    Joined:
    Jun 29, 2010
    #5
    And its name is..?
     
  6. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #6
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
  8. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    TalShani, am I wrong in assuming English is not your main language?
     
  10. TalShani thread starter macrumors member

    Joined:
    Jun 29, 2010
    #10
    Nope, I'm from israel..
     
  11. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    Getting the last character of a string is good, but now you need to compare it to a list of other characters.

    Look at NSCharacterSet to accomplish this.

    Namely...

    Code:
    extern NSString *string;
    NSCharacterSet *set = [NSCharacterSet alphanumericCharacterSet];
    if ( ![set characterIsMember: [string characterAtIndex: [string length]-1]] ){
         // code that you use to remove the last character
    }
    
     

Share This Page