Java String类常用方法(字符串比较)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-24 05:17   34   0

No.

方法名称

类型

描述

1

public boolean equals(String anObject)

普通

进行相等判断,区分大小写

2

public boolean equalsIgnoreCase(String anotherString)

普通

进行相等判断,不区分大小写

3

public int compareTo(String anotherString)

普通

判断两个字符串的大小(按照字符编码比较),此方法的返回值有如下三种操作:

·=0:表示要比较的两个字符串内容相同;

·>0:表示大于的结果。

·<0:表示小于的结果。

======================================================

public class Demo {
 public static void main(String[] args) {
  String strA = "hello";
  String strB = "hELLO";
  System.out.println(strA.equals(strB));
  System.out.println(strA.equalsIgnoreCase(strB));

 }
}

=========================================================

public class Demo {
 public static void main(String[] args) {
  String strA = "hello";
  String strB = "hELLO";
  if (strA.compareTo(strB) > 0) {
   System.out.println("大于");
  }

 }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP