How do I remove leading & trailing whitespace of NSString inside an NSArray?

The NSArray and the contained NSString objects are all immutable. There’s no way to change the objects you have.

Instead you have to create new strings and put them in a new array:

NSMutableArray *trimmedStrings = [NSMutableArray array];
for (NSString *string in arrayRefineSubjectCode) {
    NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [trimmedStrings addObject:trimmedString];
}
arrayRefineSubjectCode = trimmedStrings;

Leave a Comment