I have a couple of strings. Some have a whitespace in the beginning and some not. I want to check if a string begins with a whitespace and if so remove it.
For those who are trying to remove space in the middle of a string, use [yourString stringByReplacingOccurrencesOfString:@" " withString:@""] .
A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.
if You want to remove white spaces at start and end the you use stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] string method. For eg. NSString *s = @"0800 444 333"; s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
There is method for that in NSString class. Check stringByTrimmingCharactersInSet:(NSCharacterSet *)set
. You should use [NSCharacterSet whitespaceCharacterSet]
as parameter:
NSString *foo = @" untrimmed string "; NSString *trimmed = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With