Format address string properly

I'm querying a database which returns intersection addresses in all caps. I want to make it so the address is formatted as one would write it.

e.g. if it returns PINEHURST WAY NE & NE 115TH ST, it should be formatted as Pinehurst Way NE & NE 115th St. I've tried using the capitalizedString method for NSString but that capitalizes every word (e.g. Pinehurst Way Ne & 115Th St).

Here's my code: adStr = [adStr capitalizedString];

Is there any way I can format it the way I want it to?

Answers


There is no built-in NSString capitalizeAsOneWouldWriteAnAddress method that magically understands the logic of capitalizing the words of an address in the way you are trying to achieve. You will therefore have to achieve it manually. For example, you will have to look for compass directions such as "NE" and "SW" and make them (or keep them) all caps. And you will have to look for ordinals such as "115th" (basically, any word that starts with a digit - NSRegularExpression will come in handy here) and make sure the letters are lowercase.

You might want to look into NSLinguisticTagger to see whether it can help you tag the pieces of the address meaningfully, but I doubt that it can.


You could try something like this.

adStr = [adStr capitalizedString];

if ([string rangeOfString:@" Ne "].location != NSNotFound) {
    adStr = [adStr stringByReplacingOccurrencesOfString:@" Ne " withString:@" NE "];  
}

Try putting NE, SE, SW, etc into a array and nest the if statement in a loop.


Here's the method I used:

-(NSString *)formattedAdd:(NSString *)address {
    //Start with fresh capitalized string.
    address = [address capitalizedString];
    //Format directions properly.
    NSArray *directions = @[@"Ne", @"Se", @"Sw", @"Nw"];
    for (NSString *direc in directions) {
        if ([address rangeOfString:direc options:NSCaseInsensitiveSearch].location != NSNotFound) {
            address = [address stringByReplacingOccurrencesOfString:direc withString:[direc uppercaseString]];
        }
    }
    //Create regexes for street and numerical street names
    NSRegularExpressionOptions regexOptions = NSRegularExpressionCaseInsensitive;
    NSString *pattern = @"[0-9]\\w+";
    NSString *patterntwo = @"\\s(?:St|Ave|Pl)";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:regexOptions error:nil];
    NSRegularExpression *regextwo = [NSRegularExpression regularExpressionWithPattern:patterntwo options:regexOptions error:nil];

    //Fix numerical street names.
    NSArray *matches = [regex matchesInString:address options:0 range:NSMakeRange(0, address.length)];
    for (NSTextCheckingResult *re in matches) {
        NSString *numSt = [address substringWithRange:re.range];
        address = [address stringByReplacingCharactersInRange:re.range withString:[numSt lowercaseString]];
    }
    //Fix "St" with "St., Ave with Ave., etc."
    NSArray *matchestwo = [regextwo matchesInString:address options:0 range:NSMakeRange(0, address.length)];
    NSMutableString *adMut = [address mutableCopy];
    int count = 0;
    for (NSTextCheckingResult *re in matchestwo) {
        int index = (int)re.range.location + (int)re.range.length;
        [adMut insertString:@"." atIndex:index + (count == 1 ? 1:0];
        count++;
    }
    address = [adMut copy];
    return address;
}

Need Your Help

Drawing subplots with multiple factors in Matlab

matlab subplot factors

I have been trying to find a way to draw the following plot (obtained in JMP) in Matlab. Could anyone please let me know whether there is a way to accomplish this in Matlab?

Calling a VB.NET web service with Java

java vb.net web-services jsp

What is the best solution (if any) for calling a vb.net web service with Java / JSP? If anyone could point me to any examples / references I would very much appreciate it.