检查 NSMutableString 是否不包含特定字符
有没有办法检测 NSString 是否不包含特定字符(在本例中为“-”)?例如,如果我有 NSString @"-OU"
和 NSString @"YOU"
,有没有办法在字符串为 @ 时触发 UIAlert “YOU”
而不是 @“-OU”
?
编辑:顺便说一下,我正在尝试为任何字符串提供这种动态。我当前有以下代码,想知道这是否可以工作:
- (BOOL) isDone:(NSString *)str{
unichar dash = '-';
for(int i = 0; i < [str length]; i ++){
if([str characterAtIndex:i] != dash){
return YES;
}
else{
return NO;
}
}
}
此代码当前在 xcode 中抛出以下警告:“控件可能到达非 void 函数的末尾”。
Is there a way to detect if an NSString does not contain a specific char (in this case it's a "-")? For example, if I have the NSString @"-OU"
and the NSString @"YOU"
is there a way to fire a UIAlert when the string is @"YOU"
and not @"-OU"
?
EDIT: By the way I'm trying to make this dynamic for any string. I currently have the following code and want to know if this can work:
- (BOOL) isDone:(NSString *)str{
unichar dash = '-';
for(int i = 0; i < [str length]; i ++){
if([str characterAtIndex:i] != dash){
return YES;
}
else{
return NO;
}
}
}
This code is currently throwing the following warning in xcode: "Control may reach end of non-void function".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
try this
如果您尝试使用自己的构建函数测试破折号是否在单词中,我会这样做这样
它将循环遍历整个字符串,直到找到破折号,并且一旦找到破折号就会终止,否则,一旦完成检查整个字符串,它将返回 NO。
if your trying to test if the dash is in the word at all using your own build function i would do it this way
This way it will loop through entire string until it finds the dash, and will terminate as soon as it finds the dash, otherwise it will return NO once its done checking entire string.