如何从 nsstring 中删除子字符串? [英] How do i remove a substring from an nsstring?

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

问题描述

好的,假设我有字符串hello my name is donald"

Ok, say I have the string "hello my name is donald"

现在,我想删除从hello"到is"的所有内容问题是,my name"可以是任何东西,也可以是他的儿子"

Now, I want to remove everything from "hello" to "is" The thing is, "my name" could be anything, it could also be "his son"

所以基本上,简单地执行 stringByReplacingOccurrencesOfString 是行不通的.

So basically, simply doing stringByReplacingOccurrencesOfString won't work.

(我有 RegexLite)

(I do have RegexLite)

我该怎么做?

推荐答案

像下面这样使用它会帮助你

Use like below it will help you

NSString *hello = @"his is name is isName";
NSRange rangeSpace = [hello rangeOfString:@" " 
                                  options:NSBackwardsSearch];
NSRange isRange = [hello rangeOfString:@"is" 
                               options:NSBackwardsSearch 
                                 range:NSMakeRange(0, rangeSpace.location)];

NSString *finalResult = [NSString stringWithFormat:@"%@ %@",[hello substringToIndex:[hello rangeOfString:@" "].location],[hello substringFromIndex:isRange.location]];
NSLog(@"finalResult----%@",finalResult);

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

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