oc中怎么实现部分字符串大小写
1个回答
展开全部
字符串反转,OC本身没有自带字符串反转的直接方法,但是有字符串遍历的方法,我们可以设置遍历选项:
NSString * string = @"abcdefg hijklmn";
NSMutableString * outputString = [NSMutableString string];
// NSStringEnumerationReverse :表示反向遍历
// NSStringEnumerationByComposedCharacterSequences : 表示按字符遍历
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[outputString appendString:substring];
}];
NSLog(@"%@",outputString);
这样就实现了字符串反转
清除空格,同样使用字符串遍历:
NSString * string = @" abcdefg hijklmn s d ";
NSMutableString * outputString = [NSMutableString string];
// NSStringEnumerationByWords:将string按空格分开,并且会自动清理首尾的空格
// 这个方法会把中间多余的空格也清理掉,比如上面的字符串,s和d之间有两个空格,会变成一个空格
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[outputString appendFormat:@"%@ ",substring];
}];
// 删除我们添加的末尾的一个空格
[outputString deleteCharactersInRange:NSMakeRange(outputString.length-1, 1)];
NSLog(@"%@",outputString);
输出:
abcdefg hijklmn s d
如果你想保留字符串内的空格,那么按下面这个比较麻烦的方法:
NSString * string = @" abcdefg hijklmn s d ";
NSMutableString * outputString = [NSMutableString stringWithString:string];
__block NSInteger spaceNumber = 0;
// 先解决字符串前端的空格
[outputString enumerateSubstringsInRange:NSMakeRange(0, outputString.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if (![substring isEqualToString:@" "]) {
* stop = YES;
return ;
}
spaceNumber++;
}];
[outputString deleteCharactersInRange:NSMakeRange(0, spaceNumber)];
// 然后反向遍历,解决字符串后端的空格
spaceNumber = 0;
[outputString enumerateSubstringsInRange:NSMakeRange(0, outputString.length) options:NSStringEnumerationByComposedCharacterSequences|NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if (![substring isEqualToString:@" "]) {
* stop = YES;
return ;
}
spaceNumber++;
}];
[outputString deleteCharactersInRange:NSMakeRange(outputString.length - spaceNumber, spaceNumber)];
NSLog(@"%@",outputString);
输出:
NSString * string = @"abcdefg hijklmn";
NSMutableString * outputString = [NSMutableString string];
// NSStringEnumerationReverse :表示反向遍历
// NSStringEnumerationByComposedCharacterSequences : 表示按字符遍历
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[outputString appendString:substring];
}];
NSLog(@"%@",outputString);
这样就实现了字符串反转
清除空格,同样使用字符串遍历:
NSString * string = @" abcdefg hijklmn s d ";
NSMutableString * outputString = [NSMutableString string];
// NSStringEnumerationByWords:将string按空格分开,并且会自动清理首尾的空格
// 这个方法会把中间多余的空格也清理掉,比如上面的字符串,s和d之间有两个空格,会变成一个空格
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[outputString appendFormat:@"%@ ",substring];
}];
// 删除我们添加的末尾的一个空格
[outputString deleteCharactersInRange:NSMakeRange(outputString.length-1, 1)];
NSLog(@"%@",outputString);
输出:
abcdefg hijklmn s d
如果你想保留字符串内的空格,那么按下面这个比较麻烦的方法:
NSString * string = @" abcdefg hijklmn s d ";
NSMutableString * outputString = [NSMutableString stringWithString:string];
__block NSInteger spaceNumber = 0;
// 先解决字符串前端的空格
[outputString enumerateSubstringsInRange:NSMakeRange(0, outputString.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if (![substring isEqualToString:@" "]) {
* stop = YES;
return ;
}
spaceNumber++;
}];
[outputString deleteCharactersInRange:NSMakeRange(0, spaceNumber)];
// 然后反向遍历,解决字符串后端的空格
spaceNumber = 0;
[outputString enumerateSubstringsInRange:NSMakeRange(0, outputString.length) options:NSStringEnumerationByComposedCharacterSequences|NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if (![substring isEqualToString:@" "]) {
* stop = YES;
return ;
}
spaceNumber++;
}];
[outputString deleteCharactersInRange:NSMakeRange(outputString.length - spaceNumber, spaceNumber)];
NSLog(@"%@",outputString);
输出:
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询