gpt4 book ai didi

ios - 在 : and before? 之后拆分字符串

转载 作者:可可西里 更新时间:2023-11-01 03:31:38 26 4
gpt4 key购买 nike

我有一个像这样的长字符串:

je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?

我想在 : 之后和之前拆分字符串? .所以我想把它放入数组中:

{e,es,e,ons,ez,en}

我知道我可以做到:

 NSArray *subStrings = [stringToChange componentsSeparatedByString:@":"];

但这还不够。请问我该怎么做?任何帮助将不胜感激!

最佳答案

另一种选择是使用 NSRegularExpression类。

使用这种模式:

\\:([^\?]+)\?

示例代码:

NSString *sample  = @"je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?";
NSString *pattern = @"\\:([^\?]+)\?";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:&error];


NSArray *matches = [regex matchesInString:sample
options:0
range:NSMakeRange(0, [sample length])];
if(!error && [matches count] > 0) {
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NSLog(@"%@" ,[sample substringWithRange:matchRange]);
}
}

输出:

2015-07-03 12:16:39.037 TestRegex[3479:48301] :e
2015-07-03 12:16:39.039 TestRegex[3479:48301] :es
2015-07-03 12:16:39.039 TestRegex[3479:48301] :e
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ons
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ez
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ent

关于ios - 在 : and before? 之后拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200088/

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