Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

ios - Split string by a constant number, only on a space

I am using the answer from this question (https://stackoverflow.com/a/13854813) to split a large string into an array based on a specific length.

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

I would like to make this only split the string on a space. So, if the last character of the substring is not a space, I would like it it shorten that substring until the last character is a space (hopefully that makes sense). Basically I want this to split the string, but not split words in the process.

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

May be you can separate with componentsSeparatedByCharactersInSet: and re-construct lines.

But in your case, I think you'd better to iterate unichars.

NSMutableArray *result = [NSMutableArray array];

NSUInteger charCount = string.length;
unichar *chars = malloc(charCount*sizeof(unichar));
if(chars == NULL) {
    return nil;
}
[string getCharacters:chars];
unichar *cursor = chars;
unichar *lineStart = chars;
unichar *wordStart = chars;
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];

while(cursor < chars+charCount) {
    if([whitespaces characterIsMember:*cursor]) {
        if(cursor - lineStart >= length) {
            NSString *line = [NSString stringWithCharacters:lineStart length:wordStart - lineStart];
            [result addObject:line];
            lineStart = wordStart;
        }
        wordStart = cursor + 1;
    }
    cursor ++;
}
if(lineStart < cursor) {
    [result addObject:[NSString stringWithCharacters:lineStart length: cursor - lineStart]];
}
free(chars);
return result;

Input:

@"I would like to make this only split the string on a space. So, if the last character of the substring is not a space, I would like it it shorten that substring until the last character is a space (hopefully that makes sense). Basically I want this to split the string, but not split words in the process."

Output(length == 30):

(
    "I would like to make this ",
    "only split the string on a ",
    "space. So, if the last ",
    "character of the substring is ",
    "not a space, I would like it ",
    "it shorten that substring ",
    "until the last character is a ",
    "space (hopefully that makes ",
    "sense). Basically I want this ",
    "to split the string, but not ",
    "split words in the process."
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...