简体   繁体   English

如何将NSString分成较小的单词?

[英]How do I divide NSString into smaller words?

Greetings, 问候,

I am new to objective c, and I have the following issue: 我是目标c的新手,并且遇到以下问题:

I have a NSString: 我有一个NSString:

 "There are seven words in this phrase"

I want to divide this into 3 smaller strings (and each smaller string can be no longer than 12 characters in length) but must contain whole words separated by a space, so that I end up with: 我想将其分为3个较小的字符串(每个较小的字符串的长度不能超过12个字符),但必须包含整个单词,并用空格分隔,因此我得出以下结论:

String1 = "There are" //(length is 9 including space)
String2 = "seven words"// (length is 11)
String3 = "in this" //(length is 7), with the word "phrase" ignored as this would exceed the maximum length of 12..

Currently I am splitting my original array into an array with: 目前,我将原始数组拆分为以下数组:

NSArray *piecesOfOriginalString = [originalString componentsSeparatedByString:@" "];

Then I have multiple "if" statements to sort out situations where there are 3 words, but I want to make this more extensible for any array up to 39 (13 characters * 3 line) letters, with any characters >40 being ignored. 然后,我有多个“ if”语句来整理存在3个单词的情况,但是我想使它对于最多39个(13个字符* 3行)字母的任何数组都具有更大的可扩展性,而忽略任何大于40个字符。 Is there an easy way to divide a string based on words or "phrases" up to a certain length (in this case, 12)? 是否有一种简单的方法可以根据单词或“短语”将字符串划分为一定长度(在这种情况下为12)?

well, you can keep splitting the string as you're already doing, or you could check out whether NSScanner suits your needs. 好了,您可以继续拆分字符串,或者检查NSScanner是否满足您的需求。 In any case, you're going to have to do the math yourself. 无论如何,您都必须自己做数学。

Something similar to this? 与此相似吗? (Dry-code warning) (干码警告)

NSArray *piecesOfOriginalString = [originalString componentsSeparatedByString:@" "];

NSMutableArray *phrases  = [NSMutableArray array];
NSString *chunk = nil;
NSString *lastchunk = nil;

int i, count = [piecesOfOriginalString count];
for (i = 0; i < count; i++) {
    lastchunk = [[chunk copy] autorelease];

    if (chunk) {
        chunk = [chunk stringByAppendingString:[NSString stringWithFormat:@" %@", [piecesOfOriginalString objectAtIndex:i]]];
    } else {
        chunk = [[[piecesOfOriginalString objectAtIndex:i] copy] autorelease];
    }

    if ([chunk length] > 12) {
        [phrases addObject:lastchunk];
        chunk = nil;
    }

    if ([phrases count] == 3) {
        break;
    }
}

Thanks McLemore, that is really helpful! 感谢McLemore,这真的很有帮助! I will try this immediately. 我将立即尝试。 My current solution is very similar, but less refined, as I hard coded the loops and use individual variable to hold the sub strings (called them TopRow, MidRow, and BottomRow), that and the memory management issue is overlooked... : 我当前的解决方案非常相似,但不够完善,因为我对循环进行了硬编码,并使用单个变量来保存子字符串(称为TopRow,MidRow和BottomRow),并且忽略了内存管理问题...:

int maxLength = 12; // max chars per line (in each string)
int j=0; // for looping, j is the counter for managing the words in the "for" loop
TopRow = nil; //1st string
MidRow = nil; //2nd string
//BottomRow = nil; //third row string (not implemented yet)
BOOL Row01done = NO; // if YES, then stop trying to fill row 1
BOOL Row02done = NO; // if YES, then stop trying to fill row 2
largeArray = @"Larger string with multiple words";

tempArray = [largeArray componentsSeparatedByString:@" "];
for (j=0; j<[tempArray count]; j=j+1) {
    if (TopRow == nil) {
        TopRow = [tempArray objectAtIndex:j];
    }
    else {
        if (Row01done == YES) {
            if (MidRow == nil) {
                MidRow = [tempArray objectAtIndex:j];
            }
            else {
                if (Row02done == YES) {
                    //row 3 stuff goes here... unless I can rewrite as iterative loop...
                    //will need to uncommend BottomRow = nil; above..
                }                           
                else {
                    if ([MidRow length] + [[tempArray objectAtIndex:j] length] < maxLength) {
                        MidRow = [MidRow stringByAppendingString:@" "];
                        MidRow = [MidRow stringByAppendingString:[tempArray objectAtIndex:j]];
                    }
                    else {
                        Row02done = YES;
                        //j=j-1; // uncomment once BottowRow loop is implemented
                    }
                }
            }
        }
        else {
            if (([TopRow length] + [[tempArray objectAtIndex:j] length]) < maxLength) {
                TopRow = [TopRow stringByAppendingString:@" "];
                TopRow = [TopRow stringByAppendingString:[tempArray objectAtIndex:j]];
            }
            else {
                Row01done = YES;
                j=j-1; //end of loop without adding the string to TopRow, subtract 1 from j and start over inside Mid Row
            }
        }           
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM