0

你好我是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,我该如何删除。

先感谢您

4

3 回答 3

3

用于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不区分大小写的方式删除出现的 (无论是大写还是小写),请NSStringCompareOptions在创建 NSRange 时使用 ,如下所示:

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

其余代码相同。

于 2012-05-23T06:13:45.533 回答
0
NSString *String1 = @"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";
NSString *String2 = [String1 stringByReplacingOccurrencesOfString:@"AND" withString:@""];



NSString * aString  = @"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 = [aString rangeOfString:@"AND"];
if (replaceRange.location !=NSNotFound){
    NSString * newString = [aString stringByReplacingCharactersInRange:replaceRange withString:@""];
    NSLog(newString);
}
于 2012-05-23T06:07:57.207 回答
0
NSString *s = @"your string";
NSInteger loc = [s rangeOfString: @"AND"].location;
if (loc != NSNotFound) s = [NSString stringWithFormat: @"%@%@", [s substringToIndex: loc], [s substringFromIndex: loc + 3]];

这将删除第一次出现的 AND。第二行查找第一次出现的位置。第三行获取前后的子字符串,并使用 stringWithFormat 将它们放在一起。

获取位置后,还有其他方法可以将其删除,例如使用 给定范围的查找替换stringByReplacingCharactersInRange,或者像我的示例一样执行此操作,但使用stringByAppendingString连接部分。

于 2012-05-23T06:28:42.113 回答