Tuesday, May 14, 2013

NSString hasPrefix example ios

[NSString hasPrefix]

Returns a Boolean value that indicates whether a given string matches the beginning characters of the receiver.
- (BOOL)hasPrefix:(NSString *)aString
A string.
Return Value of [NSString hasPrefix]
YES if aString matches the beginning characters of the receiver, otherwise NO. Returns NO if aString is empty.
Discussion of [NSString hasPrefix]
This method is a convenience for comparing strings using the NSAnchoredSearch option. See String Programming Guide for more information.
Example of [NSString hasPrefix]
NSString *myURLString = @"www.google.com";
if ([myURLString hasPrefix:@"http://"]) {
    myURL = [NSURL URLWithString:myURLString];
} else {
    myURL = [NSURL URLWithString:[NSString StringWithFormat:@"http://%@",myURLString]];
Example of [NSString hasPrefix]
NSString* output = nil;
if([string hasPrefix:@"*"])
    output = [string substringFromIndex:1];
Example of [NSString hasPrefix]
NSArray *allStringsArray = 
   [myStringThatHasHttpUrls componentsSeparatedByString:@" "]

for (id myArrayElement in allStringsArray) {
    NSString *theString = [myArrayElement description];
    if ([theString hasPrefix:@"http"]) {
        NSLog(@"The URL  is %@", [myArrayElement description]);