user968597
user968597

Reputation: 1156

How to delete string after third comma ( , ) from string in iOS?

I am getting a string from web service like: @"current address is Narveer Tanaji Malusare Road, Dandekar Pool, Dattavadi, Pune, Maharashtra 411030, India". Now i want remove string after third comma so address will look like: @"current address is Narveer Tanaji Malusare Road, Dandekar Pool, Dattavadi"; and remove third comma. Please help me.

Thank You

Upvotes: 0

Views: 1075

Answers (3)

Abizern
Abizern

Reputation: 150605

I know this has been answered, but it's bothering me, so here's my solution;

Here's the method:

- (NSString *)shortenedAddress:(NSString *)address {
    // Turn the string into an array of each part separated by a comma
    NSArray *components = [address componentsSeparatedByString:@","];

    // No need to shorten the address if there are three items or less
    if ([components count] < 4) {
        return address;
    }

    // Create a range object for the first three items in the array
    NSRange range;
    range.location = 0;
    range.length = 3;

    return [[components subarrayWithRange:range] componentsJoinedByString:@","];
}

And if you test it with this:

NSString *fullAddress = @"current address is Narveer Tanaji Malusare Road, Dandekar Pool, Dattavadi, Pune, Maharashtra 411030, India"; // More than three items
NSString *partialAddress = @"current address is Narveer Tanaji Malusare Road, Dandekar Pool"; // Less than three items

NSLog(@"Full Address: %@", [self shortenedAddress:fullAddress]);
NSLog(@"Partial Address: %@", [self shortenedAddress:partialAddress]);

it gives the following output in the console:

Full Address: current address is Narveer Tanaji Malusare Road, Dandekar Pool, Dattavadi

Partial Address: current address is Narveer Tanaji Malusare Road, Dandekar Pool

I think it's shorter, easier to understand, and easier to modify than the accepted answer, but that's just my opinion.

Upvotes: 0

Rinat Khanov
Rinat Khanov

Reputation: 1576

You can achieve what you want using basic NSString methods.

I just wrote a method that does exactly what you are asking:

-(NSString *) clearStringAfterThirdComma: (NSString *) originalString {

    NSMutableString *clearedString = [originalString mutableCopy];

    NSInteger commaCount = 0;

    // iterate thru each character of the string

    for (NSInteger i = 0; i < originalString.length; i++) {

        NSString *subString = [originalString substringWithRange:NSMakeRange(i, 1)];

        if ([subString isEqualToString:@","]) {

            commaCount++;

            if (commaCount == 3) {
                [clearedString deleteCharactersInRange:NSMakeRange(i, originalString.length - i)];
                break;
            }
        }
    }

    return clearedString;
}

Upvotes: 0

meronix
meronix

Reputation: 6176

use componentsSeparatedByString:

NSString *list = @"current address is Narveer Tanaji Malusare Road, Dandekar Pool, Dattavadi, Pune, Maharashtra 411030, India";
NSArray *listItems = [list componentsSeparatedByString:@","];

// produces an array { @"Karin", @"Carrie", @"David"" }.

// here check if listItems.count >=3

NSString* reducedString = [NSString stringWithFormat:@"%@, %@, %@",[listItems objectAtIndex:0],[listItems objectAtIndex:1],[listItems objectAtIndex:2] ];

Upvotes: 2

Related Questions