按常数将字符串拆分为数组 [英] Spliting string to array by constant number

查看:104
本文介绍了按常数将字符串拆分为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试按数字将字符串拆分成多个组成部分的数组,但不知道该怎么做。我知道每个组件的长度为9,最后一个除外。但是它们之间没有分隔。也许有人会知道我怎么可能使拆分成为可能?

I'v been trying to split string to array of components by number, but have no idea how to do it. I know that each components lenght is 9 except the last one. But there is no separation between them. Maybe anyone would know how could i make this split possible?

字符串:E44000000R33000444V33441
而且我想使用以下数组:E44000000 R33000444 V33441

string : E44000000R33000444V33441 And i'd like to get array with: E44000000 R33000444 V33441

过去我曾使用过这种方法,但我想应该有一种以常数分隔的方法。任何想法

in past I'v used this method, but i guess there should be a way to separate by constant number. Any ideas

NSArray *myWords = [message componentsSeparatedByString:@";"];


推荐答案

我调查了NSString,但没有找到任何类似的功能。但是您可以创建一个NSString类别,然后将此函数放在该类别中,并且可以用作NSString实例方法。

I investigated the NSString, and i didn't found any function like that. But you can create a category of NSString and put this function in that category and you can use as a NSString instance method.

- (NSArray *) componentSaparetedByLength:(NSUInteger) length{
        NSMutableArray *array = [NSMutableArray new];
        NSRange range = NSMakeRange(0, length);
        NSString *subString = nil;
        while (range.location + range.length <= self.length) {
            subString = [self substringWithRange:range];
            [array addObject:subString];
            //Edit
            range.location = range.length + range.location;
            //Edit
            range.length = length;
        }

        if(range.location<self.length){
            subString = [self substringFromIndex:range.location];
            [array addObject:subString];
        }
        return array;
}

这篇关于按常数将字符串拆分为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆