Problem description
In the project,
- Some textfields require no Spaces at the end of the string.
- There are some strings that cannot have Spaces in them.
- Enter only letters and Numbers (control TextField to accept only characters within display control)
To solve the above situations one by one:
Solutions:
- Remove Spaces at the beginning and end of the string:
OC implementation:
Use of nsstrings stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] method only removes white Spaces at the left and right sides of;
By OC:
NSString *str = [NSString stringWithFormat:@" sda sda "];
str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
NSLog(@"%@", str);
By Swift:
var str = " sda sad "
str = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
print(str)
Remove All Spaces:
NSString *str = [NSString stringWithFormat:@" what the f**k are you doing ! "];
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@", str);