If text exists in label, in Xcode

半世苍凉 提交于 2019-12-11 16:08:30

问题


Ok so I wanna see if the user has in putted a single word among other words like for example I want the user to say hi, but not only the word hi! For example like this :

Everybody this is XCoder, say hi class. I want an if command that will scan for the word hi and delete it if it is there and if it is not do nothing to the text.

Summary : Scan for particular text, if exists substring it. or remove, I would most likely expecting or expecting a (if) statement more than anything else cause this is a static label, (changing).


回答1:


Referred from other post.. The key is noticing that rangeOfString: returns an NSRange struct, and the documentation says that it returns the struct {NSNotFound, 0} if the "haystack" does not contain the "needle".

 NSString *myString = @"Hi I am asking for help.";
    if ([string rangeOfString:@"Hi"].location == NSNotFound) {
      NSLog(@"%@",myString);
    } else {
      NSString *updated = [myString stringByReplacingOccurrencesOfString:@" am" withString:@""];
      NSLog(@"%@",updated);
    }


来源:https://stackoverflow.com/questions/25955646/if-text-exists-in-label-in-xcode

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