1.字符串比较,是按照字符串(String)中每一个字符(char)的字段表顺序进行比较

  1. /**
  2. * Compares two strings lexicographically(字典序,按照字典顺序).
  3. * The comparison is based on the Unicode value of each character in
  4. * the strings. The character sequence represented by this
  5. * {@code String} object is compared lexicographically to the
  6. * character sequence represented by the argument string. The result is
  7. * a negative integer if this {@code String} object
  8. * lexicographically precedes(先于) the argument string. The result is a
  9. * positive integer if this {@code String} object lexicographically
  10. * follows the argument string. The result is zero if the strings
  11. * are equal; {@code compareTo} returns {@code 0} exactly when
  12. * the {@link #equals(Object)} method would return {@code true}.
  13. * <p>
  14. * This is the definition of lexicographic ordering. If two strings are
  15. * different, then either they have different characters at some index
  16. * that is a valid index for both strings, or their lengths are different,
  17. * or both. If they have different characters at one or more index
  18. * positions, let <i>k</i> be the smallest such index; then the string
  19. * whose character at position <i>k</i> has the smaller value, as
  20. * determined by using the &lt; operator, lexicographically precedes the
  21. * other string. In this case, {@code compareTo} returns the
  22. * difference of the two character values at position {@code k} in
  23. * the two string -- that is, the value:
  24. * <blockquote><pre>
  25. * this.charAt(k)-anotherString.charAt(k)
  26. * </pre></blockquote>
  27. * If there is no index position at which they differ, then the shorter
  28. * string lexicographically precedes the longer string. In this case,
  29. * {@code compareTo} returns the difference of the lengths of the
  30. * strings -- that is, the value:
  31. * <blockquote><pre>
  32. * this.length()-anotherString.length()
  33. * </pre></blockquote>
  34. *
  35. * @param anotherString the {@code String} to be compared.
  36. * @return the value {@code 0} if the argument string is equal to
  37. * this string; a value less than {@code 0} if this string
  38. * is lexicographically less than the string argument; and a
  39. * value greater than {@code 0} if this string is
  40. * lexicographically greater than the string argument.
  41. */
  42. public int compareTo(String anotherString) {
  43. int len1 = value.length;
  44. int len2 = anotherString.value.length;
  45. int lim = Math.min(len1, len2);
  46. char v1[] = value;
  47. char v2[] = anotherString.value;
  48.  
  49. int k = 0;
  50. while (k < lim) {
  51. char c1 = v1[k];
  52. char c2 = v2[k];
  53. if (c1 != c2) {
  54. return c1 - c2;
  55. }
  56. k++;
  57. }
  58. return len1 - len2;
  59. }
  1. String str1 = "abd";
  2. String str2 = "aba";
  3. int res1 = str1.compareTo(str2);
  4. //
  5. System.out.println(res1);

  1. String str1 = "Abd";
  2. String str2 = "aba";
  3. int res1 = str1.compareTo(str2);
  4. //-32
  5. System.out.println(res1);

2.比较时忽略大小写

  1. /**
  2. * Compares two strings lexicographically, ignoring case
  3. * differences. This method returns an integer whose sign is that of
  4. * calling {@code compareTo} with normalized versions of the strings
  5. * where case differences have been eliminated by calling
  6. * {@code Character.toLowerCase(Character.toUpperCase(character))} on
  7. * each character.
  8. * <p>
  9. * Note that this method does <em>not</em> take locale into account,
  10. * and will result in an unsatisfactory ordering for certain locales.
  11. * The java.text package provides <em>collators</em> to allow
  12. * locale-sensitive ordering.
  13. *
  14. * @param str the {@code String} to be compared.
  15. * @return a negative integer, zero, or a positive integer as the
  16. * specified String is greater than, equal to, or less
  17. * than this String, ignoring case considerations.
  18. * @see java.text.Collator#compare(String, String)
  19. * @since 1.2
  20. */
  21. public int compareToIgnoreCase(String str) {
  22. return CASE_INSENSITIVE_ORDER.compare(this, str);
  23. }
  1. String str1 = "Abd";
  2. String str2 = "aba";
  3. int res = str1.compareToIgnoreCase(str2);
  4. //
  5. System.out.println(res);

String类对象的比较的更多相关文章

  1. 反射消除String类对象的不可变特性

    大家都知道,在JAVA中字符串一旦声明就不可改变,如果尝试修改字符串的内容,将会重新实例化一个新的字符串对象,这也是为了安全性和效率. 由于字符串在程序之中被大量使用,所以JAVA引入了一个字符串常量 ...

  2. java笔记--String类对象解析与运用

    --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877236.html "谢谢-- 1.String中的equals和==的 ...

  3. String类对象两种实例化方式比较

    第一种:直接赋值 String str =  "hello!" ; 在java中,有一个字符串常量池,对于这种直接赋值的,会直接写进常量池(常量池里面不存在其value,) 自JD ...

  4. JAVA笔记3__字符串String类/对象一对一关联

    import java.lang.String; import java.util.Scanner; public class Main { public static void main(Strin ...

  5. c++中string类对象和字符数组之间的相互转换

    string类在c++中是一个模板类,位于名字空间std中,注意这里不是string.h,string.h是C字符串头文件. 将string类型转换为字符数组char arr[10];string s ...

  6. String类对象相加时做了什么

    我们都知道java中的加号操作符除了加法.表示正数之外,还可以用作字符串的连接.初学java时,你很可能会碰到类似下面的题目: 以下这段代码产生了几个String对象: String str1 = & ...

  7. Scanner类、匿名对象、Random类、ArrayList集合、String类、static静态类、math类和Arrays工具类

    一.Scanner类 1.除了八种基本数据类型,其他都是引用类型: 引用类型使用三步骤: 2.Scanner类 引用jdk提供的类,Scanner在java.util包下,不在java.lang包(S ...

  8. String类和StringBuffer类的区别

    首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...

  9. 【Java 进阶篇】【第一课】String类

    引用 String类包含在java.lang包中.这个包会在Java启动的时候自动import,所以可以当做一个内置类(built-in class).我们不需要显式的使用import引入String ...

随机推荐

  1. jQuery 的运行机制(How jQuery Works)

    原文地址:http://learn.jquery.com/about-jquery/how-jquery-works/ linkjQuery: 基础知识 这是一个基本的教程,旨在帮助您开始使用jQue ...

  2. caffe fine tune 复制预训练model的参数和freeze指定层参数

    复制预训练model的参数,只需要重新copy一个train_val.prototxt.然后把不需要复制的层的名字改一下,如(fc7 -> fc7_new),然后fine tune即可. fre ...

  3. Oracle数据库脚本中的set define off

    2018年8月6日15:11:34 Oracle数据库脚本中的set define off 前言 最近在公司写需求,接触到脚本,第一句set define off;就不知道什么意思了,查询后记录之. ...

  4. 全链路压测平台(Quake)在美团中的实践

    背景 在美团的价值观中,以“客户为中心”被放在一个非常重要的位置,所以我们对服务出现故障越来越不能容忍.特别是目前公司业务正在高速增长阶段,每一次故障对公司来说都是一笔非常不小的损失.而整个IT基础设 ...

  5. jQuery中bind,live,delegate,on的区别

    bind(),live()都是调用on()函数,只不过传递的参数不同. 一.$(selector).bind(event,data,fn); $('#J_btn').bind('click',func ...

  6. Python实现QQ自动点赞

    用Python做一个QQ自动点赞神器,上代码: 1 def QQZan(qq): 2 browser = webdriver.Chrome() 3 browser.maximize_window() ...

  7. python 文件内容修改替换操作

    当我们读取文件中内容后,如果想要修改文件中的某一行或者某一个位置的内容,在python中是没有办法直接实现的,如果想要实现这样的操作只能先把文件所有的内容全部读取出来,然后进行匹配修改后写入到新的文件 ...

  8. python opencv3 视频文件的读写

    git: https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 读取视频文件 ...

  9. JavaSE基础之封装

    JavaSE基础之封装 一.Java中的封装 1.字面意思: 包装: 2.专业含义: 面向对象的三大特征之一: 指的是将对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象内部信息,而是通过该类所 ...

  10. Windows安装绿色版git管理软件GitStack 2.3.8

    1.原来 GitStack  是安装在局域网的,为了更好开展工作,迁移到公网的服务器.(安全性未知) 2.公网服务器已经在运行一个 Apache 2.4 (占用80端口): 3.GitStack 2. ...