gpt4 book ai didi

ios - Objective-c 每 23 个字符换行一次而不断词

转载 作者:行者123 更新时间:2023-11-28 23:52:10 27 4
gpt4 key购买 nike

我从这篇文章中找到了这个算法Objective-c Line breaks every 10 characters (Keeping words in tact)问题是它永远不会返回最后一个单词,它总是以某种方式丢弃它。

NSString *sourceStr = @"The Chocholate Experience in the Anna Ice Cream Museum - Spanish";

NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSMutableArray *stringsArray = [[NSMutableArray alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceStr];
NSString *scannedString = nil;

while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {
if ([currentLine length] + [scannedString length] <= 23) {
[currentLine appendFormat:@"%@ ", scannedString];
}
else if ([currentLine length] == 0) { // Newline but next word > 23
[resultString appendFormat:@"%@\n", scannedString];
}
else { // Need to break line and start new one
[resultString appendFormat:@"%@\n", currentLine];
[currentLine setString:[NSString stringWithFormat:@"%@ ", scannedString]];
}
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
}
NSLog(@"Result String: \n%@", resultString);

I get this as the output:
The Chocholate
Experience in the Anna
Ice Cream Museum -

去掉最后一个单词,即西类牙语。任何人都可以看到该错误吗?我已经在这几个小时了。任何帮助将不胜感激

最佳答案

如果您单步执行,您会注意到您遗漏的单词在您离开 while 循环后存储在 currentLine var 中。基本上,最后一个 else 条件将“Ice Cream Museum -”附加到 resultString,但是因为没有其他任何东西要扫描(“Spanish”已经保存在 scannedString 中——所以它已经被扫描),它只是将当前行中的单词“Spanish”,并且不会经过 while 循环的另一次迭代(因此永远不会将其放在要打印的 resultString 中)。

这是应该打印出最后一个词的修改后的代码:

NSString *sourceStr = @"The Chocholate Experience in the Anna Ice Cream Museum - Spanish";

NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSMutableArray *stringsArray = [[NSMutableArray alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceStr];
NSString *scannedString = nil;

while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {

// keep appending as long as we have something to append to
if ([currentLine length] + [scannedString length] <= 23) {
[currentLine appendFormat:@"%@ ", scannedString];
}
else if ([currentLine length] == 0) { // Newline but next word > 23
[resultString appendFormat:@"%@\n", scannedString];
}
else { // Need to break line and start new one
[resultString appendFormat:@"%@\n", currentLine];
[currentLine setString:[NSString stringWithFormat:@"%@ ", scannedString]];
}
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
}
// we may have something left over that couldn't fit into the last line
// that needs to be appended to the result string
if (currentLine.length > 0) {
[resultString appendFormat:@"%@\n", currentLine];
}
NSLog(@"Result String: \n%@", resultString);

关于ios - Objective-c 每 23 个字符换行一次而不断词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51885668/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com