Brijesh Singh Brijesh Singh - 1 year ago 102
iOS Question

How can i trim spaces from between of a NSString while displaying in UI Label

I am getting the string value from web service.I need to remove spaces from between of a string. How can I do that?
Example:if string is

it must become
while displaying the value in UILabel text.

I have attached the below image for clarification.

enter image description here

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download