Java源码-字符串大小比较(Comparing Strings)

注:这篇博文当时写的时候有点问题,今天(2020-02-13)已修正。但是文章摘要还是修改前的内容,特此说明。

代码如下:

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-斌

 

附:

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 the equals(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()
 
Specified by: compareTo(...) in Comparable

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.

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值