gpt4 book ai didi

ios - 从 NSString 获取一个子串直到到达一个特定的词

转载 作者:IT王子 更新时间:2023-10-29 08:10:11 25 4
gpt4 key购买 nike

假设我有这个 NSString:@"Country Address Tel:number"。如何获取 Tel 之前的子字符串? (国家地址)然后如何获取 Tel 之后的子字符串? (数量)

最佳答案

使用 NSScanner:

NSString *string = @"Country Address Tel:number";
NSString *match = @"tel:";
NSString *preTel;
NSString *postTel;

NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner scanUpToString:match intoString:&preTel];

[scanner scanString:match intoString:nil];
postTel = [string substringFromIndex:scanner.scanLocation];

NSLog(@"preTel: %@", preTel);
NSLog(@"postTel: %@", postTel);

NSLog 输出:

preTel: Country Address
postTel: number

关于ios - 从 NSString 获取一个子串直到到达一个特定的词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9243196/

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