简体   繁体   English

与两个字符串数组iPhone比较

[英]compare with two string array iphone

i have created a quiz application for iphone Where i added multiple selection option. 我为iphone创建了一个测验应用程序,在其中添加了多个选择选项。 Suppose correct ans is ABC and selected ans is AC. 假设正确的ans是ABC,选择的ans是AC。 Both are string array. 两者都是字符串数组。 how can i compare if the ans is correct or wrong. 我该如何比较ans是正确还是错误。

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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