字符串compareTo()方法 (String compareTo() Method)

compareTo() is a String method in Java and it is used to compare two strings (case-sensitive).

compareTo()是Java中的String方法,用于比较两个字符串(区分大小写)。

If both strings are equal – it returns 0, otherwise, it returns a value less than 0 or greater than 0 based on the first dissimilar characters difference.

如果两个字符串相等–返回0 ,否则,根据第一个不同的字符差返回小于0大于0的值。

Syntax:

句法:

    int string1.compareTo(string2);

Here, string1 and string2 are the strings to be compared, and it returns an integer value that is 0, less than 0 or greater than 0.

在这里, string1和string2是要比较的字符串,它返回一个整数值,该值是0,小于0或大于0。

Example:

例:

    Input:
str1 = "Hello world!"
str2 = "Hello world!"
Output:
0
Input:
str1 = "Hello world!"
str2 = "HELLO WORLD!"
Output:
32

Java code to compare strings using String.compareTo() method

Java代码使用String.compareTo()方法比较字符串

public class Main
{public static void main(String[] args) {String str1 = "Hello world!";
String str2 = "Hello world!";
String str3 = "HELLO WORLD!";
System.out.println("str1.compareTo(str2) = " + str1.compareTo(str2));
System.out.println("str1.compareTo(str3) = " + str1.compareTo(str3));
System.out.println("str2.compareTo(str3) = " + str2.compareTo(str3));
//checking with the condition
if(str1.compareTo(str2)==0){System.out.println("str1 is equal to str2");
}
else{System.out.println("str1 is not equal to str2");
}
if(str1.compareTo(str3)==0){System.out.println("str1 is equal to str3");
}
else{System.out.println("str1 is not equal to str3");
}
if(str2.compareTo(str3)==0){System.out.println("str2 is equal to str3");
}
else{System.out.println("str2 is not equal to str3");
}
}
}

Output

输出量

str1.compareTo(str2) = 0
str1.compareTo(str3) = 32
str2.compareTo(str3) = 32
str1 is equal to str2
str1 is not equal to str3
str2 is not equal to str3

翻译自: https://www.includehelp.com/java/string-compareTo-method-with-example.aspx

Java String compareTo()方法与示例相关推荐

  1. java 获取字符串长度_ava练习实例:java字符串长度与Java String charAt() 方法 (建议收藏)...

    JJava 字符串长度 public class StringDemo {public static void main(String args[]) {String palindrome = &qu ...

  2. Java Date compareTo()方法具有什么功能呢?

    转自: Java Date compareTo()方法具有什么功能呢? Date compareTo()方法的功能简介说明:对比两个Date值的大小Date compareTo()方法的语法:publ ...

  3. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  4. Java中CompareTo()方法的详细介绍

    Java中CompareTo()方法: String 字符串用compareTo方法从第一位开始比较, 如果遇到不同的字符,则马上返回这两个字符的ascii值差值.返回值是int类型 1.当两个比较的 ...

  5. 2021-12-27 Java String contains() 方法用于判断字符串中是否包含指定的字符或字符串。用.toLowerCase().contains忽视大小写。

    一.public boolean contains(CharSequence chars) contains() 方法用于判断字符串中是否包含指定的字符或字符串. 二.返回值        如果包含指 ...

  6. Java String compareToIgnoreCase() 方法

    Java有一组可以用于字符串的内置方法.Java 字符串(String)操作常用操作,如字符串的替换.删除.截取.赋值.连接.比较.查找.分割等.本文主要介绍Java String compareTo ...

  7. Java String equalsIgnoreCase()方法的功能说明

    转自: Java String equalsIgnoreCase()方法具有什么功能呢? Java String equalsIgnoreCase()方法的功能说明 Java String equal ...

  8. Java String split方法性能分析

    近期笔者分析一段在线代码的性能时,发现Java String类中split()方法的性能和网上一些帖子的描述并不相同,是以记录澄清. Java String split方法到底有没有使用正则表达式? ...

  9. java compareto long_Java Long类compareTo()方法与示例

    Long类compareTo()方法compareTo()方法在java.lang包中可用. compareTo()方法用于在数学上检查该Long对象与给定Long对象的相等性或不相等性,换句话说,可 ...

最新文章

  1. 《世界上最伟大的50种思维方法》总结
  2. 关于spring MVC中加载多个validator的方法。
  3. ICE专题:ICE简介
  4. easyui datagrid 中怎么选中所有页面的数据_学会这5个Excel中常用技巧,可以准时下班去摆摊了...
  5. 八、栈的操作、栈空间
  6. 因政府禁令 微软 Windows 9 将作出重大调整
  7. CentOS 7.0安装配置Vsftp服务器
  8. 相当于jQuery .hide()来设置可见性:隐藏
  9. 『中级篇』Docker-Stack部署wordpress(49)
  10. 调用目标检测百度接口api
  11. OpenCV-图像处理(08、模糊图像一)
  12. 【台州世通兰陵王系列】服务器的1U、2U、4U是指什么?
  13. AWS实例3T磁盘初始化分区及docker环境部署
  14. 一瓶纯粮酱香型白酒的成本是多少?
  15. 冯诺依曼计算机体系结构
  16. c语言if else嵌套,C语言if else嵌套
  17. ns2 + GT-ITM
  18. Navicat使用timestamp类型,设置默认使用当前时间
  19. java removeat,在c#中 RemoveAt、 Remove、delete用法区别
  20. python录音文件降噪_Python谱减法语音降噪实例

热门文章

  1. 比特币 源代码_GitHub遭黑客攻击:窃取数百源码并勒索比特币,数量惊人!
  2. 这张磁盘有写保护_架构师不得不了解的硬件知识 - 磁盘阵列RAID
  3. android 高并发弹幕,高并发实时直播弹幕研发实践
  4. rssi定位算法 c语言,【论文※】An RSSI Gradient-based AP Localization Algorithm 基于RSSI梯度的AP定位算法...
  5. pytorch如何计算导数_PyTorch怎么用?来看这里
  6. 数据结构知识点总结_大牛带你学 | 考研数据结构中线性表中顺序结构的知识点总结...
  7. 大整数算术求值 c语言 栈,用C语言实现 多位整数的四则运算,用栈,例如56*(12+20)-102/2...
  8. python垃圾回收机制为什么标记能解决循环引用问题_python 关于循环引用以及标记清除的问题...
  9. Linux添加新硬盘-挂载硬盘,设置开机自动挂载 解决/home 空间不足问题
  10. DAY3-“忙里偷闲”找你玩耍2018-1-11