【问题标题】:Why is this substring problem returning false? [duplicate]为什么这个子字符串问题返回假? [复制]
【发布时间】:2015-06-10 11:02:33
【问题描述】:

为什么这是假的?

public class practice 
{
   public static void main(String [] args) 
   {
      System.out.println(startHi("hi "));
   }

   public static boolean  startHi(String str) 
   {
       System.out.println(str.substring(0,2));
       if(str.length() < 2) 
       {
          return false;
       }
       else if(str.substring(0,2) ==("hi")) 
       {
          return true;
       }
       else 
       {
         return false;
       }
   }
}

【问题讨论】:

    标签: java methods substring


    【解决方案1】:

    您应该使用.equals 方法来检查字符串是否相等,而不是==See here.

    使用== 检查对象是否在内存中具有相同的地址。在检查两个字符串的值是否相同时,这通常不是您要查找的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多