gpt4 book ai didi

iphone - 如何从字符串中删除一个单词

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

你好,我是 objective-c 的初学者我有一个这样的 nsstring

Select *  from Store_Master where AND  VDivison = 'Casual Dining' AND VBrand_Name = 'Pinkberry' AND VCountry_Name = 'Egypt' AND VCity = 'Cairo' AND VBuilding_mall_name = 'Dandy Mega Mall' GROUP BY VStore_Name

我只想删除第一次出现的 AND,我该如何删除。

提前致谢

最佳答案

使用 stringByReplacingCharactersInRange 仅替换第一个“AND”:

NSString* s = @"Select *  from Store_Master where AND  VDivison = 'Casual Dining' AND VBrand_Name = 'Pinkberry' AND VCountry_Name = 'Egypt' AND VCity = 'Cairo' AND VBuilding_mall_name = 'Dandy Mega Mall' GROUP BY VStore_Name";
NSRange replaceRange = [s rangeOfString:@"AND"];
if (replaceRange.location != NSNotFound){
NSString* result = [s stringByReplacingCharactersInRange:replaceRange withString:@""];
}

这会导致:

Select *  from Store_Master where   VDivison = 'Casual Dining' AND VBrand_Name = 'Pinkberry' AND VCountry_Name = 'Egypt' AND VCity = 'Cairo' AND VBuilding_mall_name = 'Dandy Mega Mall' GROUP BY VStore_Name

编辑:

如果你想以不区分大小写的方式删除 AND 的出现(无论大写还是小写),请在创建 NSRange 时使用 NSStringCompareOptions,如下所示:

NSRange replaceRange = [s rangeOfString:@"and" options:NSCaseInsensitiveSearch];

其余代码相同。

关于iphone - 如何从字符串中删除一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10714323/

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