字符串相等(==)的全测试

本文通过一个Java示例程序详细解析了字符串在不同情况下使用“==”进行比较的行为。包括相同包内不同类间的字符串常量引用、不同包内的字符串常量引用以及运行时计算生成的字符串之间的比较。
  1. package p1;
  2. /**
  3.  * 字符串相等(==)的全测试。
  4.  * 
  5.  * @author XT Zang,老紫竹
  6.  */
  7. class TestString1 {
  8.   public static void main(String[] args) {
  9.     String hello = "Hello", lo = "lo";
  10.     System.out.print((hello == "Hello") + " "); // true
  11.     System.out.print((Other.hello == hello) + " "); // true
  12.     System.out.print((p2.Other.hello == hello) + " "); // true
  13.     System.out.print((hello == ("Hel" + "lo")) + " "); // true
  14.     System.out.print((hello == ("Hel" + lo)) + " "); // false
  15.     System.out.println(hello == ("Hel" + lo).intern()); // true
  16.   }
  17. }
  18. class Other {
  19.   static String hello = "Hello";
  20. }
  1. package p2;
  2. public class Other {
  3.   public static String hello = "Hello";
  4. }
结论:

This example illustrates six points:

  • Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).相同包,相同类的字符串常量指向相同的字符串对象

  • Literal strings within different classes in the same package represent references to the same String object.相同包,不相同类的字符串常量指向相同的字符串对象

  • Literal strings within different classes in different packages likewise represent references to the same String object.不同包,不同类的字符串常量指向相同的字符串对象

  • Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.字符串在编译期间可以连接起来的,视同一个常量字符串

  • Strings computed by concatenation at run time are newly created and therefore distinct.

The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.


参考地址:http://blog.youkuaiyun.com/ZangXT/archive/2008/10/11/3057471.aspx

### 正确判断字符串相等的方法 在编程中,不同语言有其特定的方式来比较字符串是否相等。以下是几种常见编程语言中的正确方法: #### Python 中的字符串比较 Python 提供了简单的语法来比较字符串是否相等。应使用 `==` 运算符来进行比较操作[^1]。如果需要区分大小写的严格比较,则直接使用 `==` 即可;如果不区分大小写,可以先将两者转换为相同的大小写形式再进行比较。 ```python test_str = "Good" if test_str.lower() == "good": print("Strings are equal (case-insensitive).") else: print("Strings are not equal.") ``` 需要注意的是,在条件语句中避免像下面这样的错误写法: ```python if test_str == 'good' or 'happy': # 错误写法 ``` 上述代码实际上总是返回 True,因为 `'happy'` 是一个非空字符串,会被解释为布尔值 True。 #### C/C++ 中的字符串比较 C 和 C++ 的标准库提供了函数用于字符串比较。对于 C 需要调用 `<string.h>` 头文件里的 `strcmp()` 函数[^4]。而 C++ 可以利用 STL 中的 std::string 类型自带的成员函数或者重载的 `==` 运算符完成此功能。 ```cpp #include <iostream> #include <cstring> int main(){ const char* str1 = "hello"; const char* str2 = "world"; if(strcmp(str1, str2) == 0){ std::cout << "Strings are equal." << std::endl; } else { std::cout << "Strings are different." << std::endl; } return 0; } ``` #### Java 中的字符串比较 Java 不推荐通过 `==` 对象引用做比较,而是应该采用 `.equals()` 方法来检测内容上的平等关系。 ```java String s1 = new String("abc"); String s2 = new String("abc"); // Wrong way to compare strings. if(s1 == s2){ System.out.println("Equal"); } // Correct method using equals(). if(s1.equals(s2)){ System.out.println("Equal by content."); } ``` #### MyBatis 动态 SQL 字符串比较 当涉及到数据库查询时,MyBatis框架下的 `<if>` 标签允许开发者基于参数是否存在以及它们的内容构建灵活的SQL片段[^3]。为了防止潜在的空指针异常或逻辑失误,建议如下设置测试条件: ```xml <if test="_parameter != null and _parameter.trim() != ''"> AND column_name=#{_parameter} </if> ``` 这里 `_parameter.trim()` 去除了首尾空白字符后再参与对比,从而提高匹配准确性并减少不必要的干扰项。 #### Qt QString 比较方式 Qt 库提供两种主要途径去判定两个 QString 是否一致:一是运用 `operator==` ,二是借助于 `QString::compare()` 成员函数[^2]。前者执行字面意义上的逐字符对照,后者则支持更多选项比如忽略字母大小写差异等附加特性。 ```cpp QString qStrA = "Example", qStrB = "example"; bool isEqualCaseInsensitive = (qStrA.compare(qStrB, Qt::CaseInsensitive)==0); ``` 以上就是针对多种主流开发环境里如何恰当地实施字符串一致性核查的技术要点总结。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值