检查 NSMutableString 是否不包含特定字符

发布于 2024-11-02 08:25:27 字数 562 浏览 0 评论 0原文

有没有办法检测 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-11-09 08:25:27

试试这个

NSString *yourString=@"-OU";  
if ([yourString rangeOfString:@"-"].location == NSNotFound) {
  NSLog(@"string does not contain -");
} else {
  NSLog(@"string contains -");
}

try this

NSString *yourString=@"-OU";  
if ([yourString rangeOfString:@"-"].location == NSNotFound) {
  NSLog(@"string does not contain -");
} else {
  NSLog(@"string contains -");
}
多彩岁月 2024-11-09 08:25:27

如果您尝试使用自己的构建函数测试破折号是否在单词中,我会这样做这样

 - (BOOL) isDone:(NSString *)str{

       unichar dash = '-';

    for(int i = 0; i < [str length]; i ++){
        if([str characterAtIndex:i] == dash){
            return YES;
        }

    }
    return NO;

}

它将循环遍历整个字符串,直到找到破折号,并且一旦找到破折号就会终止,否则,一旦完成检查整个字符串,它将返回 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

 - (BOOL) isDone:(NSString *)str{

       unichar dash = '-';

    for(int i = 0; i < [str length]; i ++){
        if([str characterAtIndex:i] == dash){
            return YES;
        }

    }
    return NO;

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文