compareTo()方法如何比较字符串? [英] How does the compareTo() method, compare strings?

查看:725
本文介绍了compareTo()方法如何比较字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要比较字符串Hello和World。

Such as if I were to compare the Strings "Hello" and "World".

它如何知道Hello比世界更大?

How does it know Hello is greater than World?

我唯一想到的是,也许它使用ASCII表作为参考?

The only thing I can come up with is, maybe it uses the ASCII Table as reference?

感谢您的帮助!

推荐答案

它比较两个字符串 lexographically
字符串API

it compares two strings lexographically. check here in the String API.


如果两个字符串不同,则它们具有不同的
字符在某个索引处,它是两个字符串的有效索引,或者
,它们的长度不同,或两者兼而有之。如果他们在一个或多个索引位置有不同的
字符,那么让k为最小的
索引;然后是位置k的字符具有较小的
值的字符串,通过使用<运算符,按字典顺序
在另一个字符串之前。

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 position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string.

这篇关于compareTo()方法如何比较字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆