Rumah  >  Artikel  >  Java  >  详解compareToIgnoreCase的返回值的原理

详解compareToIgnoreCase的返回值的原理

Y2J
Y2Jasal
2017-05-17 10:31:452469semak imbas

int n1="This".compareToIgnoreCase("tHe");
int n2="JAVA".compareToIgnoreCase("javaline")
int n3="Java".compareToIgnoreCase("Javits")

结果是n1=4,n2=-4,n3=-8,

这是为什么呢?原因是这样的:

n1:"This" 与 "tHe"比较,从第三个字符开始不同,i比e大4
n2:前面四个字符相同,

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value: 
 this.length()-anotherString.length()
n3:同n1

【相关推荐】

1. 特别推荐“php程序员工具箱”V0.1版本下载

2. Java免费视频教程

3. 关于compareToIgnoreCase()方法的详细介绍

4.compareToIgnoreCase()不区分大小写比较两个字符串

5.Java经典字符串比较的方法:compareToIgnoreCase()

6.深入了解compareTo与comparetoIgnorecase之间的不同之处

Atas ialah kandungan terperinci 详解compareToIgnoreCase的返回值的原理. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn