gpt4 book ai didi

ios - 从 Objective-C 中的字符串中删除短单词和字符

转载 作者:行者123 更新时间:2023-11-28 18:35:34 25 4
gpt4 key购买 nike

我有一个字符串可能是这样的:

NSString *string = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry";

我希望我的结果是:

"Lorem Ipsum simply dummy printing typesetting industry"

我的第一个想法:

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"some regex magic" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:@"Lorem Ipsum is simply dummy text of the printing and typesetting industry" options:0 range:NSMakeRange(0, [firstLine length]) withTemplate:@""];
NSLog(@"%@", modifiedString);

最佳答案

嗯,我想这一定有无数种解决方案。这是其中之一:

NSString *string = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry";
NSArray *words = [string componentsSeparatedByString:@" "];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"length > 4"];
NSArray *largerWords = [words filteredArrayUsingPredicate:pred];
NSString *filteredString = [largerWords componentsJoinedByString:@" "];

NSLog(@"%@", filteredString);
// Outputs => Lorem Ipsum simply dummy text printing typesetting industry

关于ios - 从 Objective-C 中的字符串中删除短单词和字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19770837/

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