温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

字符串分割小例子

发布时间:2020-07-24 09:17:09 来源:网络 阅读:264 作者:iSanny 栏目:开发技术
一个反向输出字符串的例子,不用componentsSeparatedByString去实现以空格字符分割字符串然后反向输出:


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

        @autoreleasepool {
                
                // insert code here...
                //NSLog(@"Hello, World!");
                NSString *a =[[NSString alloc] initWithFormat:@"I am a good person\n"];
                NSMutableString *letters = [NSMutableString stringWithFormat:@""];
                //NSInteger t = 0;
                for (NSInteger i = a.length-1;i>=0; i--)
                {
                        if([a characterAtIndex:i]==' ')
                        {
                                NSString *temp = [a substringWithRange:NSMakeRange(i+1,a.length-i-1)];
                                //NSLog(@"%@",temp);
                                for(NSInteger j=0;j<temp.length;j++)
                                {
                                        if([temp characterAtIndex:j]==' ' || [temp characterAtIndex:j]=='\n')
                                        {NSString *temp2= [temp substringWithRange:NSMakeRange(0, j)];
                                                [letters appendString:temp2];
                                                [letters appendString:@" "];
                                                //NSLog(@"%@",temp2);
                                                break;
                                        }
                                }
                        }
                        if(i==0)
                        {
                                [letters appendString:[a substringWithRange:NSMakeRange(0,i+1)]];
                        }
                }
        NSLog(@"%@",letters);
        }
        
        return 0;
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI