字符串分割小例子-创新互联

一个反向输出字符串的例子,不用componentsSeparatedByString去实现以空格字符分割字符串然后反向输出:

#import

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                                {
                                        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;
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

创新互联专注于德令哈网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供德令哈营销型网站建设,德令哈网站制作、德令哈网页设计、德令哈网站官网定制、成都微信小程序服务,打造德令哈网络公司原创品牌,更为您提供德令哈网站排名全网营销落地服务。
分享题目:字符串分割小例子-创新互联
文章起源:http://cdiso.cn/article/dchidg.html

其他资讯