how to remove a word from a string

泄露秘密 提交于 2019-12-11 18:49:30

问题


hello i am a beginner in objective-c i have a nsstring like this

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

i want to just remove first occurance of AND,how can i remove.

Thank you in advance


回答1:


Use stringByReplacingCharactersInRange to replace only the first '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:@""];
}

That would result in:

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

EDITED:

If you want to remove the occurrences of AND in a case insensitive way (no matter uppercase of lowercase), use the NSStringCompareOptions when you create the NSRange, like this:

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

The rest of the code is the same.




回答2:


NSString *s = @"your string";
NSInteger loc = [s rangeOfString: @"AND"].location;
if (loc != NSNotFound) s = [NSString stringWithFormat: @"%@%@", [s substringToIndex: loc], [s substringFromIndex: loc + 3]];

This will remove the first occurrence of AND. The second line finds the location of the first occurrence. The third line gets the substring before and after and puts them together using stringWithFormat.

After getting the location, there are other ways to do remove it, such as find-replace given a range using stringByReplacingCharactersInRange, or doing it like my example but using stringByAppendingString to concatenate the parts.




回答3:


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);
}


来源:https://stackoverflow.com/questions/10714323/how-to-remove-a-word-from-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!