xcode iOS compare strings

前端 未结 5 2051
北恋
北恋 2020-12-14 15:37

How do i compare a website result with a predicted result.

@\"document.getElementsByTagName(\'body\')[0].outerHTML\"

is predicted to contai

相关标签:
5条回答
  • 2020-12-14 15:51

    for Swift 4.0:

    if str1==str2 {
        //both string are equal 
    }
    
    else {
        //do something expression not true 
    }
    

    for Objective-C:

    if ([str1 isEqualToString:str2]) {
        //both string are equal 
    }
    
    else {
        //do something expression not true 
    }
    
    0 讨论(0)
  • 2020-12-14 15:57
    if (webresult == cmp)
    

    Here, == checks whether webresult, cmp are pointing to the same reference or not. You should instead compare value of the object by using NSString::isEqualToString.

     if ( [ cmp isEqualToString:webresult ]) {
       // ..
     }else {
       // ..
     }
    

    Note that isEqualToString is a good option because it returns boolean value.

    0 讨论(0)
  • 2020-12-14 15:59

    I assume that webresult is an NSString. If that is the case, then you want to use:

    if ([webresult isEqualToString:cmp]) {
    

    instead of:

    if (webresult == cmp) {
    

    as the above method checks if the strings are equal character by character, whereas the bottom method checks if the two strings are the same pointer. Hope that helps!

    0 讨论(0)
  • 2020-12-14 16:00

    We cannot comapre the strings with ==
    We have to use isEqualToString:

    if([str1 isEqualToString:str2])
    {
    }
    else
    {
    }
    
    0 讨论(0)
  • 2020-12-14 16:04

    When comparing strings you need to use isEqualToString:

    if ([cmp isEqualToString:webresult]) {
       ...
    } else {
       ...
    }
    
    0 讨论(0)
提交回复
热议问题