HenryGale
HenryGale

Reputation: 681

Replace string characters between 2 specific characters

I'm trying to scrub an NSString for an email to remove the domain name.

So let's say

"[email protected]"

would need to be

"testEmail@****.com"

OR

testEmail@**** (I also need to have a method that replaces all the characters after the "@"

but for any domain variation.

If anyone has simple solution I would really appreciate it.

Thanks!

Upvotes: 0

Views: 668

Answers (4)

Peter Foti
Peter Foti

Reputation: 5654

This should do the trick, would make a nice category method.

- (NSString *)stringByReplacingEmailDomainWithAsterisks:(NSString *)emailAddress
{
    NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"@."];
    NSArray *allComponents = [emailAddress componentsSeparatedByCharactersInSet:charSet];

    NSMutableString *newString = [NSMutableString new];
    [newString appendFormat:@"%@@", [allComponents firstObject]];

    NSRange range = NSMakeRange(0, 1);
    for (__unused int i = range.location; range.location < [allComponents[1] length]; range.location++) {
        [newString appendString:@"*"];
    }
    [newString appendFormat:@".%@", allComponents[2]];

    return [NSString stringWithFormat:@"%@", newString];
}

Example:

NSString *someEmail = @"[email protected]";

NSLog(@"%@", [self stringByReplacingEmailDomainWithAsterisks:someEmail]);

Returns:

This_isAtest@*******.com

Edit: You would need to add in some error checking to make sure you got a good string/email, etc.

Upvotes: 1

warrenm
warrenm

Reputation: 31782

This is by no means bulletproof, but it's a bit more robust than the previous answers.

- (NSString *)sanitizedStringForEmailAddress:(NSString *)address elideEntireDomain:(BOOL)elideEntireDomain
{
    NSRange asperandRange = [address rangeOfString:@"@" options:NSBackwardsSearch];
    NSRange domainRange = [address rangeOfString:@"." options:NSBackwardsSearch];

    if (asperandRange.location != NSNotFound &&
        domainRange.location != NSNotFound &&
        asperandRange.location < domainRange.location)
    {
        NSRange elisionRange = NSMakeRange(asperandRange.location + 1, domainRange.location - asperandRange.location - 1);
        if (elideEntireDomain)
        {
            elisionRange.length = address.length - asperandRange.location - 1;
        }

        NSString *elisionString = @"****";

        return [address stringByReplacingCharactersInRange:elisionRange withString:elisionString];
    }

    return nil;
}

Remember that there's no restriction against the "local" part of an email address containing "@" as long as the local portion is quoted, per RFC 2822.

Upvotes: 2

rebello95
rebello95

Reputation: 8576

For replacing the domain with 4 asterisks:

NSString *email = @"[email protected]";

NSRange at = [email rangeOfString:@"@"];
NSRange com = [email rangeOfString:@".com"];
email = [email stringByReplacingCharactersInRange:NSMakeRange(at.location + at.length, com.location - at.location - at.length) withString:@"****"];

And for replacing everything after the @:

NSString *email = @"[email protected]";

NSRange at = [email rangeOfString:@"@"];
email = [email stringByReplacingCharactersInRange:NSMakeRange(at.location + at.length, email.length - at.location - at.length) withString:@"****"];

Upvotes: 0

Josh Blade
Josh Blade

Reputation: 981

string email = "[email protected]";
int atIndex = email.IndexOf('@');
string scrubbed = email.substring(0, atIndex + 1) + "****.com"

That grabs all of the characters up to the @ and then appends "****.com" to the end.

Upvotes: 0

Related Questions