// if((btn.currentTitle == answerBtn.currentTitle) && btn.hidden == YES)
// 字符串相等比较 不要直接比,这样比的是指针,不是指针指向的数据
if([btn.currentTitle isEqualToString:answerBtn.currentTitle] && btn.hidden == YES)

NSString *strA = [NSString stringWithFormat:@"a"];

NSString *strB = [NSString stringWithFormat:@"b"];

if( strA == strB)

    NSLog(@"A is equal to B");

    else

    NSLog(@"A is not equal to B");

运行这段code, 在console 上的输出是: A is not equal to B

代码做些改动, 将 strA 与strB 设为相等。

NSString *strA = [NSString stringWithFormat:@"a"];

NSString *strB = [NSString stringWithFormat:@"a"];

if( strA == strB)

    NSLog(@"A is equal to B");

    else

    NSLog(@"A is not equal to B");

运行这段code ,在console上的输出仍然是 A is not equal to B 。

这是为什么呢 ?问题出在 字符串对比的语句上。

if ( strA == strB) // 这个strA, strB 是指针, 虽然字符串的内容是相同的, 但指向字符串的 指针肯定是不同的, 也不能相同啊。 (为了更好地理解字符串,需要弄清楚 指针的概念。 内存的分配。 )

// 错误:if( strA == strB)

// 正确: if ([strA isEqualToString:strB])

iOS SDK 本身 也提供了 字符串对比的方法: isEqualToString:

判断字符串是否相等 isEqualToString:的更多相关文章

  1. iOS 判断字符串是否为空

    写一个字符串的扩展,实现判断字符串是否为空- (BOOL) isBlankString { if ([self isEqualToString:@"(null)"]) { retu ...

  2. C# 判断字符串是否是int/double

    using System.Text.RegularExpressions; /// <summary> /// 判断字符串是否是int/double /// </summary> ...

  3. C#判断字符串是否是数字

    /// <summary> /// 判断字符串是否是数字 /// </summary> public static bool IsNumber(string s) { if ( ...

  4. JS判断字符串长度的5个方法

    这篇文章主要介绍了JS判断字符串长度的5个方法,并且区分中文和英文,需要的朋友可以参考下 目的:计算字符串长度(英文占1个字符,中文汉字占2个字符)   方法一:    代码如下: String.pr ...

  5. C++ 判断字符串是否全是数字

    在实际的工作中,需要提取程序中的字符串信息,但是程序中经常将一些数字当做字符串来进行处理,例如表盘的刻度信息,这时候就需要判断字符串是否全为数字,来进行真正意义上的字符串提取.下面介绍了判断字符串是否 ...

  6. Shell判断字符串包含关系的几种方法

    现在每次分析网站日志的时候都需要判断百度蜘蛛是不是真实的蜘蛛,nslookup之后需要判断结果中是否包含“baidu”字符串 以下给出一些shell中判断字符串包含的方法,来源程序员问答网站 stac ...

  7. Android 判断字符串是否为空

    TextUtils.isEmpty(str) 可以判断字符串是否为null或者"",当是的时候为true,否的时候为false

  8. 用递归法判断字符串A中包含多少个字符串B

    string类提供了判断字符串B在字符串A中首次(或最后)出现的Index的方法,但有时候需要判断B在A中出现了多少次. 为此想了一个算法. public static void CountIndex ...

  9. PHP判断字符串中是否包含指定字符串,支持中文哦

    RT,随手写的 /** * 判断字符串中是否包含指定字符串 * @var source 源字符串 * @var target 要判断的是否包含的字符串 * @return bool */ functi ...

随机推荐

  1. git submodule初用

    git submodule主要是用于针对git项目中还存在git子模块的情况.在一般情况下,我们通过git clone 获取项目的时候会把项目中的所有信息都拿到.但是,如果相关中存在git子模块那么, ...

  2. Java程序设计之Constructor

    插入段代码,下次回忆吧. 先新建一个Person类,代码如下: public class Person { private String name ; private int age; public ...

  3. Linux之shell篇

    shell是用户与系统交互的界面,这是基本方式之一.标准的shell为bash. shell的操作: 显示所有使用过的命令:history. 执行最近执行过的一条指令:!!.首先会给出执行的是哪一条指 ...

  4. Openvpn 撤销签署的证书(删除用户)

    https://wiki.archlinux.org/index.php/Easy-rsa Revoking certificates and alerting the OpenVPN server ...

  5. Node6.9.2 —— Http官网笔记整理

    欢迎指导与讨论 : ) 序章 本文概要:http.Agent代理.http.ClientRequest客户端请求.http.server服务器.http.ServerResponse服务器相应.htt ...

  6. 《中国文明史》系列—外柔 VS 内厉

    读启良的<中国文明史>,里面有谈到外柔而内厉——中国政府自古以来奉行的准则.大致意思是说,我华夏民族对待周边民族,历来是很友好的,即所谓的“柔”,而对待自己人,向来是“刚”或曰“厉”的. ...

  7. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  8. [LeetCode] Graph Valid Tree 图验证树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  9. [LeetCode] Delete Duplicate Emails 删除重复邮箱

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  10. [LeetCode] Symmetric Tree 判断对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...