Consider the following example:
" Hello this is a long string! "
I want to convert that to:
"Hello this is a long string!"
Use the native regexp solution provided by hfossli.
Either use your favorite regexp library or use the following Cocoa-native solution:
NSString *theString = @" Hello this is a long string! ";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
theString = [filteredArray componentsJoinedByString:@" "];
Regex and NSCharacterSet is here to help you. This solution trims leading and trailing whitespace as well as multiple whitespaces.
NSString *original = @" Hello this is a long string! ";
NSString *squashed = [original stringByReplacingOccurrencesOfString:@"[ ]+"
withString:@" "
options:NSRegularExpressionSearch
range:NSMakeRange(0, original.length)];
NSString *final = [squashed stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Logging final
gives
"Hello this is a long string!"
Possible alternative regex patterns:
[ ]+
[ \\t]+
\\s+
Ease of extension, performance, number lines of code and the number of objects created makes this solution appropriate.
stringByReplacingOccurrencesOfString:
. Can't believe I didn't know that.
Commented
Aug 14, 2013 at 1:24
Actually, there's a very simple solution to that:
NSString *string = @" spaces in front and at the end ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@", trimmedString)
(Source)
With a regex, but without the need for any external framework:
NSString *theString = @" Hello this is a long string! ";
theString = [theString stringByReplacingOccurrencesOfString:@" +" withString:@" "
options:NSRegularExpressionSearch
range:NSMakeRange(0, theString.length)];
NSRegularExpressionSearch
says that it only works with the rangeOfString:...
methods
Commented
Jul 5, 2011 at 19:56
A one line solution:
NSString *whitespaceString = @" String with whitespaces ";
NSString *trimmedString = [whitespaceString
stringByReplacingOccurrencesOfString:@" " withString:@""];
This should do it...
NSString *s = @"this is a string with lots of white space";
NSArray *comps = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableArray *words = [NSMutableArray array];
for(NSString *comp in comps) {
if([comp length] > 1)) {
[words addObject:comp];
}
}
NSString *result = [words componentsJoinedByString:@" "];
Another option for regex is RegexKitLite, which is very easy to embed in an iPhone project:
[theString stringByReplacingOccurencesOfRegex:@" +" withString:@" "];
Try This
NSString *theString = @" Hello this is a long string! ";
while ([theString rangeOfString:@" "].location != NSNotFound) {
theString = [theString stringByReplacingOccurrencesOfString:@" " withString:@" "];
}
Here's a snippet from an NSString
extension, where "self"
is the NSString
instance. It can be used to collapse contiguous whitespace into a single space by passing in [NSCharacterSet whitespaceAndNewlineCharacterSet]
and ' '
to the two arguments.
- (NSString *) stringCollapsingCharacterSet: (NSCharacterSet *) characterSet toCharacter: (unichar) ch {
int fullLength = [self length];
int length = 0;
unichar *newString = malloc(sizeof(unichar) * (fullLength + 1));
BOOL isInCharset = NO;
for (int i = 0; i < fullLength; i++) {
unichar thisChar = [self characterAtIndex: i];
if ([characterSet characterIsMember: thisChar]) {
isInCharset = YES;
}
else {
if (isInCharset) {
newString[length++] = ch;
}
newString[length++] = thisChar;
isInCharset = NO;
}
}
newString[length] = '\0';
NSString *result = [NSString stringWithCharacters: newString length: length];
free(newString);
return result;
}
Alternative solution: get yourself a copy of OgreKit (the Cocoa regular expressions library).
The whole function is then:
NSString *theStringTrimmed =
[theString stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
OGRegularExpression *regex =
[OGRegularExpression regularExpressionWithString:@"\s+"];
return [regex replaceAllMatchesInString:theStringTrimmed withString:@" "]);
Short and sweet.
If you're after the fastest solution, a carefully constructed series of instructions using NSScanner
would probably work best but that'd only be necessary if you plan to process huge (many megabytes) blocks of text.
according from @Mathieu Godart is best answer, but some line is missing , all answers just reduce space between words , but when if have tabs or have tab in place space , like this: " this is text \t , and\tTab between , so on " in three line code we will : the string we want reduce white spaces
NSString * str_aLine = @" this is text \t , and\tTab between , so on ";
// replace tabs to space
str_aLine = [str_aLine stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
// reduce spaces to one space
str_aLine = [str_aLine stringByReplacingOccurrencesOfString:@" +" withString:@" "
options:NSRegularExpressionSearch
range:NSMakeRange(0, str_aLine.length)];
// trim begin and end from white spaces
str_aLine = [str_aLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
the result is
"this is text , and Tab between , so on"
without replacing tab the resul will be:
"this is text , and Tab between , so on"
You can also use a simple while argument. There is no RegEx magic in there, so maybe it is easier to understand and alter in the future:
while([yourNSStringObject replaceOccurrencesOfString:@" "
withString:@" "
options:0
range:NSMakeRange(0, [yourNSStringObject length])] > 0);
Following two regular expressions would work depending on the requirements
Then apply nsstring's instance method stringByReplacingOccurrencesOfString:withString:options:range:
to replace them with a single white space.
e.g.
[string stringByReplacingOccurrencesOfString:regex withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, [string length])];
Note: I did not use 'RegexKitLite' library for the above functionality for iOS 5.x and above.