ios 如何检测 NSString 是否包含特定字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14266059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:48:55  来源:igfitidea点击:

How to detect if NSString contains a particular character or not?

iosobjective-c

提问by Sim Fox

i have one NSString object , for ex:- ($45,0000)

我有一个 NSString 对象,例如:- ($45,0000)

Now i want to find if this string contains () or not

现在我想找出这个字符串是否包含 ()

How can i do this?

我怎样才能做到这一点?

回答by Lily Ballard

Are you trying to find if it contains at least one of (or )? You can use -rangeOfCharacterFromSet::

您是否正在尝试查找它是否至少包含(或 之一)?您可以使用-rangeOfCharacterFromSet:

NSCharacterSet *cset = [NSCharacterSet characterSetWithCharactersInString:@"()"];
NSRange range = [mystr rangeOfCharacterFromSet:cset];
if (range.location == NSNotFound) {
    // no ( or ) in the string
} else {
    // ( or ) are present
}

回答by dloomb

The method below will return Yes if the given string contains the given char

如果给定的字符串包含给定的字符,下面的方法将返回 Yes

-(BOOL)doesString:(NSString *)string containCharacter:(char)character
{
    return [string rangeOfString:[NSString stringWithFormat:@"%c",character]].location != NSNotFound;
}

You can use it as follows:

您可以按如下方式使用它:

 NSString *s = @"abcdefg";

    if ([self doesString:s containCharacter:'a'])
        NSLog(@"'a' found");
    else
        NSLog(@"No 'a' found");


    if ([self doesString:s containCharacter:'h'])
        NSLog(@"'h' found");
    else
        NSLog(@"No 'h' found");

Output:

输出:

2013-01-11 11:15:03.830 CharFinder[17539:c07] 'a' found

2013-01-11 11:15:03.830 CharFinder [17539:c07] 'a' 发现

2013-01-11 11:15:03.831 CharFinder[17539:c07] No 'h' found

2013-01-11 11:15:03.831 CharFinder[17539:c07] 没有找到“h”

回答by Christian Hartmann

- (bool) contains: (NSString*) substring {
    NSRange range = [self rangeOfString:substring];
    return range.location != NSNotFound;
}

回答by Rahul Mathur

I got a generalized answer to your question which I was using in my code. This code includes the following rules: 1. No special characters 2. At least one capital and one small English alphabet 3. At least one numeric digit

我在我的代码中使用了您的问题的一般答案。此代码包括以下规则: 1. 无特殊字符 2. 至少一个大写字母和一个小英文字母 3. 至少一个数字

 BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter;
    int asciiValue;
    if([txtPassword.text length] >= 5)
    {
        for (int i = 0; i < [txtPassword.text length]; i++)
        {
            unichar c = [txtPassword.text characterAtIndex:i];
            if(!lowerCaseLetter)
            {
                lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];
            }
            if(!upperCaseLetter)
            {
                upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
            }
            if(!digit)
            {
                digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
            }

            asciiValue = [txtPassword.text characterAtIndex:i];
            NSLog(@"ascii value---%d",asciiValue);
            if((asciiValue >=33&&asciiValue < 47)||(asciiValue>=58 && asciiValue<=64)||(asciiValue>=91 && asciiValue<=96)||(asciiValue>=91 && asciiValue<=96))
            {
                specialCharacter=1;
            }
            else
            {
                specialCharacter=0;
            }

        }

        if(specialCharacter==0 && digit && lowerCaseLetter && upperCaseLetter)
        {
            //do what u want
            NSLog(@"Valid Password %d",specialCharacter);
        }
        else
        {
            NSLog(@"Invalid Password %d",specialCharacter);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and No Any special character"
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }

回答by Pravin Kamble

Why to always use for NSCharactorSet ?? this is simple and powerful solution

为什么总是使用 NSCharactorSet ??这是简单而强大的解决方案

NSString *textStr = @"This is String Containing / Character";

if ([textStr containsString:@"/"])
{
    NSLog(@"Found!!");
}
else
{
   NSLog(@"Not Found!!");

回答by Alex Obenauer

You can use

您可以使用

NSString rangeOfString: (NSString *) string

See here: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html%23//apple_ref/occ/instm/NSString/rangeOfString:

请参阅此处:https: //developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html%23//apple_ref/occ/instm/NSString/rangeOfString

If the NSRange comes back with property 'location' equal to NSNotFound, then the string does not contain the passed string (or character).

如果 NSRange 返回的属性“location”等于 NSNotFound,则该字符串不包含传递的字符串(或字符)。