Java源码-字符串大小比较(Comparing Strings)
来源:互联网 发布:济南seo小武 编辑:程序博客网 时间:2024/06/09 14:50
通过这个例子,发现了JavaDoc中的一个问题:
compareTo方法返回的结果按理和两个字符串在字典中的先后位置一致,即,小的排前,这个说法并不适合中文。
如,“海”和“斌”,中文字典排序(按汉语拼音),“海”在“斌”之前,但compareTo返回的结果是“海”大于“斌”。
可见,文档毕竟是文档,正确性有待实践的检验。
附:
JavaDoc中对compareTo方法的说明如下:
int java.lang.String.compareTo(String anotherString)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by thisString
object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if thisString
object lexicographically precedes the argument string. The result is a positive integer if thisString
object lexicographically follows the argument string. The result is zero if the strings are equal;compareTo
returns 0
exactly when theequals(Object)
method would returntrue
.
This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at positionk has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case,compareTo
returns the difference of the two character values at positionk
in the two string -- that is, the value:
this.charAt(k)-anotherString.charAt(k)
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()
- Parameters:
- anotherString the
String
to be compared. - Returns:
- the value
0
if the argument string is equal to this string; a value less than0
if this string is lexicographically less than the string argument; and a value greater than0
if this string is lexicographically greater than the string argument.
代码如下:
import java.util.Scanner;/**Java how to program, 10th edition * 14.3 (Comparing Strings) Write an application that uses String method compareTo * to compare two strings input by the user. Output whether the first string is * less than, equal to or greater than the second. * @author pandenghuang@163.com*/public class CompareTo { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("请输入字符串1:"); String s1=input.nextLine(); System.out.print("请输入字符串2:"); String s2=input.nextLine(); int result=s1.compareTo(s2); if (result>0) System.out.printf("字符串1-%s大于字符串2-%s%n",s1,s2); else if (result<0) System.out.printf("字符串1-%s小于字符串2-%s%n",s1,s2); else System.out.printf("比较结果:字符串1-%s等于字符串2-%s%n",s1,s2); } }
运行结果:
请输入字符串1:海
请输入字符串2:斌
字符串1-海大于字符串2-斌
- Java源码-字符串大小比较(Comparing Strings)
- Java源码-判断两个字符串的子字符串是否匹配(Comparing Portions of Strings)
- Comparing Strings
- Comparing Strings
- java字符串比较大小
- java字符串比较大小
- lintcode 比较字符串(Compare Strings ) (Java)
- java数字字符串比较大小
- Java中的字符串大小比较
- Comparing strings (C)
- A. Comparing Strings
- CodeForce 616A Comparing Two Long Integers 字符串比较
- Java中的字符串比较相等与大小
- java 中字符串的比较大小
- java字符串比较大小compareTo方法
- Java中字符串相等与大小比较
- Java中字符串相等与大小比较
- java中两个字符串比较大小
- 不小心删除PowerDesigner里面的一个小工具,怎么恢复
- View和View的参数传递
- HTTP头域列表与解释 之 request篇
- 深入简单Java类(一对多映射
- Win7系统安装MySQL5.5.21图解教程
- Java源码-字符串大小比较(Comparing Strings)
- FastJson:常规使用,积少成多:
- 梳理caffe代码loss(二十二)
- 简单Java类多对多映射
- 如何实现定时执行php的程序
- eclipse搭建andriod开发环境
- Java中对象比较(第三个代码模型)
- 179.The TRANS_SUMMARY table contains product-wise transaction details that get updated with every tr
- 解决:Error:Execution failed for task app:predexdebug