English 中文(简体)
如何从字符串中删除单词
原标题:how to remove a word from a string

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

我想先排除第一次出现的事 怎样才能排除

提前感谢您

最佳回答

使用 stringByRepectingCharacters InRange 来替换仅第一层和:

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

编辑:

如果您想要在不敏感的情况下删除 的出现( 无关紧要的小写大写), 请在创建 NSRange 时使用 < code> NSStringCompareops , 例如 :

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

代码的其余部分是相同的。

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

这将删除 AND 的第一次发生 。 第二行会找到第一次发生的地点 。 第三行会得到子字符串前后, 并用字符串ANDFormat 把它们组合在一起 。

获取位置后, 还有其他方法可以删除它, 例如使用 < code>stringingByReplactingCharactersInRange 来替换查找替换给定区域, 或者用我的示例来类似它, 但使用 stringByAputString 来连接部件 。

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




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签