简体   繁体   中英

compare with two string array iphone

i have created a quiz application for iphone Where i added multiple selection option. Suppose correct ans is ABC and selected ans is AC. Both are string array. how can i compare if the ans is correct or wrong.

try this code:

    NSString* rgtAns = @"ABC";//right answer
    NSString* ans = @"Ac";//current answer

    rgtAns = [rgtAns uppercaseString];//uppercase the string
    ans = [ans uppercaseString];//if the answer letters are uppercase, you do not need this

    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString: rgtAns] invertedSet];
    NSString *filtered = [[ans componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    BOOL isRight = [ans isEqualToString:filtered];
    NSLog(@"%d",isRight);

在iOS中,您应该使用NSString(或其可变版本NSMutableString)来处理字符串,NSString有一个名为isEqualToString:的方法isEqualToString:可帮助您比较两个字符串对象。

   if ([originalAnswer isEqualToString:userAnswerToCompare]){
   //do some processing here
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM