按常量分割字符串,仅在空格上 [英] Split string by a constant number, only on a space
问题描述
我正在使用这个问题的答案 (https://stackoverflow.com/a/13854813) 来拆分一个大的根据特定长度将字符串转换为数组.
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.
有什么建议吗?
推荐答案
也许你可以用 componentsSeparatedByCharactersInSet:
分开并重新构造行.
May be you can separate with componentsSeparatedByCharactersInSet:
and re-construct lines.
但在你的情况下,我认为你最好迭代 unichar
s.
But in your case, I think you'd better to iterate unichar
s.
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;
输入:
@"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."
输出(长度 == 30):
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."
)
这篇关于按常量分割字符串,仅在空格上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!