122 objective-c nsstring ios
请考虑以下示例:
" Hello this is a long string! "
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
"Hello this is a long string!"
Run Code Online (Sandbox Code Playgroud)
Geo*_*lly 124
使用hfossli提供的本机regexp解决方案.
使用您喜欢的regexp库或使用以下Cocoa原生解决方案:
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:@" "];
Run Code Online (Sandbox Code Playgroud)
hfo*_*sli 52
正则表达式和NSCharacterSet可以帮助您.此解决方案修剪前导和尾随空白以及多个空白.
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]];
Run Code Online (Sandbox Code Playgroud)
记录final给出
"Hello this is a long string!"
Run Code Online (Sandbox Code Playgroud)
可能的替代正则表达式模式:
[ ]+[ \\t]+\\s+易于扩展,性能,代码行数和创建的对象数使该解决方案合适.
ari*_*kfr 41
实际上,有一个非常简单的解决方案:
NSString *string = @" spaces in front and at the end ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@", trimmedString)
Run Code Online (Sandbox Code Playgroud)
(来源)
Mon*_*art 13
使用正则表达式,但不需要任何外部框架:
NSString *theString = @" Hello this is a long string! ";
theString = [theString stringByReplacingOccurrencesOfString:@" +" withString:@" "
options:NSRegularExpressionSearch
range:NSMakeRange(0, theString.length)];
Run Code Online (Sandbox Code Playgroud)
小智 9
一线解决方案:
NSString *whitespaceString = @" String with whitespaces ";
NSString *trimmedString = [whitespaceString
stringByReplacingOccurrencesOfString:@" " withString:@""];
Run Code Online (Sandbox Code Playgroud)
这应该做到......
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:@" "];
Run Code Online (Sandbox Code Playgroud)
这是扩展程序的片段NSString,其中"self"是NSString实例。它可用于通过传入[NSCharacterSet whitespaceAndNewlineCharacterSet]和' '两个参数来将连续的空白折叠为单个空格。
- (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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63450 次 |
| 最近记录: |