Java String.compareTo()方法

 描述:java.lang.String.compareTo() 方法比较两个字符串的字典。

比较是基于字符串中的每个字符的Unicode值。此String对象表示的字符序列的

参数字符串表示的字符序列进行比较字典.描述:

 1 package com.yiibai;
 2 
 3 import java.lang.*;
 4 
 5 public class StringDemo {
 6 
 7   public static void main(String[] args) {
 8   
 9     String str1 = "tutorials", str2 = "point";
10 
11     // comparing str1 and str2
12     int retval = str1.compareTo(str2);
13 
14     // prints the return value of the comparison
15     if (retval < 0) {
16        System.out.println("str1 is greater than str2");
17     }
18         
19     else if (retval == 0) {
20        System.out.println("str1 is equal to str2");
21     }
22         
23     else {
24        System.out.println("str1 is less than str2");
25     }
26   }
27 }

返回值:此方法如果这个字符串是等参数字符串那么返回值0,
如果这个字符串是按字典顺序小于字符串参数那么返回小于0的值,
如果此字符串是按字典顺序大于字符串参数那么一个大于0的值
原文地址:https://www.cnblogs.com/tdcqma/p/4828962.html