NSString *yourString = @"A ALL YEAR";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [yourString componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
yourString = [filteredArray componentsJoinedByString:@" "];
Output:- A ALL YEAR
Hope you need this. If this doesn't work, let me know.
let str = "A All Time".components(separatedBy: " ")
let newString = str[0] + " " + str[1] + " " + str[3]
You can also use a for loop.
Try NSRegularExpression
class with this regex pattern:
\s{2,}
and replace is with space
.
Example of NSRegualrExpression for string replacement .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.