如何从字符串中删除单词 [英] how to remove a word from a string

查看:44
本文介绍了如何从字符串中删除单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是Objective-C的初学者我有这样的nsstring

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

我只想删除AND的首次出现,该如何删除.

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

提前谢谢

推荐答案

使用 stringByReplacingCharactersInRange 仅替换第一个"AND":

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:@""];
}

这将导致:

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 ,如下所示:

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];

其余代码相同.

这篇关于如何从字符串中删除单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆