Check if NSString hasPrefix that is contained in NSArray
Asked Answered
T

4

2

I have this if statement to check if a URL starts with a prefix:

NSString *baseUrl = @"http://example.com/";

NSString *currentURL = @"http://example.com/confirm";

if( [currentURL hasPrefix:[baseUrl stringByAppendingString:@"confirm"]] ){

    // True, so do something...

}

I would like to modify the hasPrefix part of my if statement so that it returns true if currentUrl is appended by ANY of the values in my NSArray shown here:

NSArray *pages = [NSArray arrayWithObjects:@"confirm",@"products",nil];

How can I do this?

Turban answered 2/8, 2013 at 14:38 Comment(0)
F
1

just iterate through your pages array and set a boolean flag if any of the terms exist

//
//  customstring.m
//
//  Created by rbertsch8 on 8/2/13.
//
//

#import "customstring.h"

@implementation customstring

-(bool)hasPrefixArray:(NSArray*)array withbaseURL:(NSString*)baseUrl
{
    bool hasprefix = NO;
    for(int i = 0; i < [array count] || hasprefix == NO; i++)
    {
        hasprefix = [self hasPrefix:[baseUrl stringByAppendingString:[array objectAtIndex:i]]];
    }

    return hasprefix;
}

@end

Now in your code do the following: //import your custom string file #import NSStringPrefix.h

NSStringPrefix *customURL = yoururl.com
NSString *baseUrl = baseurl.com
NSArray *yourarray = [[NSArray alloc] initWithObjects: @"one", @"two"]

if([customURL hasPrefixArray:yourarray withbaseURL:baseUrl])
{
    //dowork
}

UPDATE

Define the variables in a global class.

#define companyOrSiteName @"Company X"
#define baseUrl @"http://www.example.com/" // Set base URL for site.
#define customUrlScheme @"example://" // Set custom URL Scheme for app.
#define openInModal [NSArray arrayWithObjects: @"contact-us.php",@"products/",nil]
#define openInSafari [NSArray arrayWithObjects: @"about",@"products/resources/download",nil]
#define twitterHandle @"Example" // Twitter username without the "@" symbol
#define kTrackingId @"UA-4564564-3" // Set Google Analytics iOS App Tracking code.
Firedog answered 2/8, 2013 at 14:43 Comment(8)
I'm looking for a solution that can be placed inline, as this will need to be used multiple times in different files. Is there a way to just modify the hasPrefix ?Turban
in this case you will need to create a custom NSString class and override the hasprefix method (or make a new method like hasPrefixArray) so make a new file -> objective c class - > subclass of NSString we'll name it NSStringPrefix in this class make a method: -(bool)hasPrefixArray:(NSArray*)array Then you just add #import NSStringPrefix.h to your code and your currentURL variable will now be NSStringPrefix *currentURLFiredog
I understand creating the files, but can you put in a block of code demonstrating with an example? Thanks!Turban
i edited my code above to show how you could implement a new hasprefixarray method into your custom NSStringPrefix classFiredog
So how would I use it?Turban
let us continue this discussion in chatFiredog
So which files do I need to create?Turban
don't use a custom subclass, just add a category to NSStringPlumbing
W
0
NSArray *parts = [currentUrl componentsSeparatedByString:@"/"];
NSString *name = [parts objectAtIndex:1];

After this you can check if the string name is a element in pages array.

Winsor answered 2/8, 2013 at 14:49 Comment(2)
I'm not sure what you mean - How would I check if the string name is an element in the pages array? That's the question and your answer doesn't show that.Turban
I meant by looping over the elements in the array and comparing them with the string name. Or alternatively u can use a NSDictionary to store the elements of the array to get a O(1) retrieval.Winsor
M
0

For the ones who want to avoid Carpal Tunnel Syndrome:

NSString *baseUrl = @"http://example.com/";
NSString *currentURL = @"http://example.com/confirm";
NSArray *pages = [NSArray arrayWithObjects:@"confirm",@"products",nil];
BOOL hasSuffix = NO;
[pages enumerateObjectsUsingBlock:^(NSString* page, NSUInteger idx, BOOL *stop) {
  if([currentURL hasPrefix: baseUrl] && [currentURL hasSuffix: page]){
    hasSuffix = YES;
    stop = YES;
  }
}];

Your description sounds more like you are looking for a suffix, so there you have it.

Merrile answered 4/6, 2014 at 18:45 Comment(0)
W
0

It's simple, use following code

BOOL hasPrefixInArray = NO;

for(int i = 0; i < [array count] && hasPrefixInArray == NO; i++)
{
    hasPrefixInArray = [yourString hasPrefix:array[i]];
}

// check value here for hasPrefixInArray
NSLog(@"%d",hasPrefixInArray);
Waterside answered 3/1, 2019 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.