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

TalShani

macrumors member
Original poster
Jun 29, 2010
45
0
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:
 
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.
 
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.

And how I can get the full length of the string?
 
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
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.