Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the compareTo method in Java string

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use the compareTo method in Java string". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the compareTo method in Java string".

First, java.lang.Comparable interface

The Comparable interface forces the sorting of the list of implementation class objects. Its sorting is called natural order, and its compareTo method is called natural comparison method.

Public interface Comparable {public int compareTo (To);}

If you use this to represent the object currently calling the compareTo method, obj is the parameter passed in by the method

Then:

This

< obj ---- 返回负数 this = obj ---- 返回 0 this >

Obj-returns a positive number

The compareTo of the Comparable interface is an internal comparison, that is, it supports comparison with the current object

Second, java.util.Comparator interface

Comparator can be thought of as an external comparator. An object does not support comparison with itself (it does not implement the Comparable interface), but wants to compare the two objects.

Public interface Comparator {int compare (T1, T2); / / omitted.}

Comparative logic:

O1

< o2 ---- 返回负数 o1 = o2 ---- 返回 0 o1 >

O2-return positive number three to talk about the compareTo method in string

What is implemented in String is the Comparable interface to make comparison logic for String objects

Public final class String implements java.io.Serializable, Comparable, CharSequence {/ /. }

Let's first look at an example:

/ * string comparison case * / public class StringComparisonDemo {public static void main (String [] args) {String foo = "ABC"; / / each character before and after is exactly the same, returning 0 String bar01 = "ABC"; System.out.println (foo.compareTo (bar01)) / / every character before it is exactly the same. Return: string length difference String bar02 = "ABCD"; String bar03 = "ABCDE"; System.out.println (foo.compareTo (bar02)); / /-1 (equal in front, foo length 1) System.out.println (foo.compareTo (bar03)) / /-2 (equal before, foo length 2) / / each character before it is not exactly the same. Return: different characters ASCII difference String bar04 = "ABD"; String bar05 = "aABCD"; System.out.println (foo.compareTo (bar04)); / /-1 (the ASCII value of foo's'C' character is 67Jing 04 'character ASCII value is 68. Return 67-68 =-1) System.out.println (foo.compareTo (bar05)); / /-32 (the ASCII code value of the'A' character of foo is 65, and the ASCII value of the'a' character of bar04 is 97. Return 65-97 =-32) String bysocket01 = "bricklayer"; String bysocket02 = "bricklayer"; System.out.println (bysocket01.compareTo (bysocket02)); / /-2049 (Unicode difference between mud and tile)}}

Results:

0

-1

-2

-1

-32

-2049

Combined with the above example, let's take a look at the implementation of compareTo method in String.

Public int compareTo (String anotherString) {/ / len1: current string length int len1 = value.length; / / len2: parameter string length int len2 = anotherString.value.length; / / len1 and len2 minimum values int lim = Math.min (len1, len2); / / converted to character array char v1 [] = value; char v2 [] = anotherString.value, respectively Int k = 0; / / comparison logic while (k < lim) {char C1 = v1 [k]; char c2 = v2 [k]; / / if the characters are different, the difference if (C1! = c2) {return C1-c2;} kcodes of the two characters is returned. } / / if the same value is returned, the difference between the length of two characters return len1-len2;}

So as you can see from the source code above, the compareTo logic in string can be sorted out as follows

Each character in the first part of the string is exactly the same, returning: the length difference between the last two strings

Each character in the first part of the string is different. Return: the difference of the different character ASCII code.

Each character of the string is exactly the same, returning 0

There is also a static inner class CaseInsensitiveComparator within String that also implements this interface.

Private static class CaseInsensitiveComparator implements Comparator, java.io.Serializable {/ /. }

The overridden interface method is the case-insensitive comparison method of the String object

Public int compare (String S1, String S2) {int N1 = s1.length (); int N2 = s2.length (); int min = Math.min (N1, N2); for (int I = 0; I < min; I +) {char C1 = s1.charAt (I); char c2 = s2.charAt (I) / / turn to uppercase if (C1! = c2) {C1 = Character.toUpperCase (C1); c2 = Character.toUpperCase (c2); / / change to lowercase if (C1! = c2) {C1 = Character.toLowerCase (C1) C2 = Character.toLowerCase (c2); / / is not the same: returns the difference of ASCII codes with different characters. If (C1! = c2) {/ / No overflow because of numeric promotion return C1-c2;} return N1-N2 Thank you for your reading, the above is the content of "how to use the compareTo method in Java string". After the study of this article, I believe you have a deeper understanding of how to use the compareTo method in Java string, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report