is there a simple way to remove the extra spaces in a string? ie like...
NSString *str = @"this string has extra empty spaces";
result should be:
NSString *str = @"this string has extra empty spaces";
Thanks!
[ QString::simplified ] Returns a string that has whitespace removed from the start and the end, and that has each sequence of internal whitespace replaced with a single space. Once the string is simplified, the white spaces can easily be removed. Option 2: Use a QRegExp to capture all types of white space in remove .
To remove all leading whitespaces, use the following code: var filtered = "" var isLeading = true for character in string { if character. isWhitespace && isLeading { continue } else { isLeading = false filtered.
stringByTrimmingCharactersInSet only removes characters from the beginning and the end of the string, not the ones in the middle. 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.
replace all double space with a single space until there are no more double spaces in your string.
- (NSString *)stripDoubleSpaceFrom:(NSString *)str {
while ([str rangeOfString:@" "].location != NSNotFound) {
str = [str stringByReplacingOccurrencesOfString:@" " withString:@" "];
}
return str;
}
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