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

时间:2009-11-09 04:18:39

标签: objective-c arrays nsstring

问候,

我是目标c的新手,我有以下问题:

我有一个NSString:

 "There are seven words in this phrase"

我想把它分成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..

目前我正在将原始数组拆分为数组:

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

然后我有多个“if”语句来排序有3个单词的情况,但是我希望这对任何最多39个(13个字符* 3行)字母的数组更加可扩展,任何字符都> 40被忽略了。是否有一种简单的方法可以将基于单词或“短语”的字符串分成一定长度(在本例中为12)?

3 个答案:

答案 0 :(得分:2)

好吧,您可以像现在一样继续分割字符串,或者可以查看NSScanner是否适合您的需求。无论如何,你将不得不自己做数学。

答案 1 :(得分:2)

类似的东西? (干码警告)

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

答案 2 :(得分:0)

谢谢McLemore,这真的很有帮助!我马上试试。我当前的解决方案非常相似,但不太精确,因为我对循环进行了硬编码并使用单个变量来保存子字符串(称为TopRow,MidRow和BottomRow),并且内存管理问题被忽略了......:< / p>

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