Tuesday, May 14, 2013

NSString hasSuffix example ios


[NSString hasSuffix]

Returns a Boolean value that indicates whether a given string matches the ending characters of the receiver.
- (BOOL)hasSuffix:(NSString *)aString
Parameters of [NSString hasSuffix]
aString
A string.
Return Value of [NSString hasSuffix]
YES if aString matches the ending characters of the receiver, otherwise NO. Returns NO if aString is empty.
Discussion of [NSString hasSuffix]
This method is a convenience for comparing strings using the NSAnchoredSearch and NSBackwardsSearch options. See String Programming Guide for more information.
Example of [NSString hasSuffix]
NSString *data = @"abcd,";
if([data hasSuffix:@","]) // This returns true in this example
    // do something
Example of [NSString hasSuffix]
if ([[yourString pathExtension] isEqualToString:@"jpg"]){
   //.jpg
}
or
if ([yourString hasSuffix:@".jpg"]){
   //.jpg
}
Example of [NSString hasSuffix]
float inchesInField = [sizeField.text floatValue];
float shoeSize = inchesInField * 3 - 22;
NSMutableString *appendedShoeSize = [[NSMutableString alloc] initWithFormat:@"%.1f", shoeSize];

if ([appendedShoeSize hasSuffix:@".3"] || [appendedShoeSize hasSuffix:@".5"] || [appendedShoeSize hasSuffix:@".4"] || [appendedShoeSize hasSuffix:@".6"]) {

    appendedShoeSize = [[appendedShoeSize substringToIndex:[appendedShoeSize length] - 2] mutableCopy];
    [appendedShoeSize appendString:@" ½"];
} 

if ([appendedShoeSize hasSuffix:@".0"] || [appendedShoeSize hasSuffix:@".1"] || [appendedShoeSize hasSuffix:@".2"]) {

    appendedShoeSize = [[appendedShoeSize substringToIndex:[appendedShoeSize length] - 2] mutableCopy];
}