对两个字符串怎么比较大小啊

yang_zhi 2010-03-23 12:19:42
我用了compareTo()当时它好像只能比较数字,如果是非数字怎么比较啊,我不比较长度,我是要一个一个的字符进行比较.那位大哥给解决一下,小弟将感激不尽.
...全文
6179 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
echolxl2010 2010-04-07
  • 打赏
  • 举报
回复
用equals
hijiankang 2010-04-07
  • 打赏
  • 举报
回复
重写compareto()方法 你想怎么比较 在方法里面就怎么写
davis_rly 2010-04-07
  • 打赏
  • 举报
回复
优先级从高到低:空格大于大写字符大于小写字符
zuoguodang 2010-04-07
  • 打赏
  • 举报
回复
使用String里面的方法就行了,如果更复杂就需要实现其他接口了
nidearong 2010-04-07
  • 打赏
  • 举报
回复
比较也可以用equals,来比较 他们是否相等。
Dazzlingwinter 2010-04-07
  • 打赏
  • 举报
回复
API~~~
jacky花园 2010-04-07
  • 打赏
  • 举报
回复
我记得考SCJP的时候有一道题是比较字母大小:
结论,字母的先后顺序和大小写决定字母的大小
24K純帥 2010-03-23
  • 打赏
  • 举报
回复
compareTo()可以比较字符串的额
tcsnzsa 2010-03-23
  • 打赏
  • 举报
回复
源码很简单,建议自己看源码。
xinguohit 2010-03-23
  • 打赏
  • 举报
回复
String中compaerTo()方法就是用来比较两个字符串的。通过比较各个相应位字符的Unicode编码值来判断大小,若两个字符串完全相同则返回0。看下面一个例子就知道了:

public class StringCompare {
public static void main(String[] args) {
String s = "abc";
String s1 = "bca";
String s2 = "cab";
String s3 = "abc";
System.out.println(s.compareTo(s1));
System.out.println(s.compareTo(s2));
System.out.println(s.compareTo(s3));
System.out.println(s2.compareTo(s1));
}
}


打印结果分别是:
-1
-2
0
1
aSysBang 2010-03-23
  • 打赏
  • 举报
回复
/**
* Compares two strings lexicographically.
* The comparison is based on the Unicode value of each character in
* the strings. The character sequence represented by this
* <code>String</code> object is compared lexicographically to the
* character sequence represented by the argument string. The result is
* a negative integer if this <code>String</code> object
* lexicographically precedes the argument string. The result is a
* positive integer if this <code>String</code> object lexicographically
* follows the argument string. The result is zero if the strings
* are equal; <code>compareTo</code> returns <code>0</code> exactly when
* the {@link #equals(Object)} method would return <code>true</code>.
* <p>
* 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 <i>k</i> be the smallest such index; then the string
* whose character at position <i>k</i> has the smaller value, as
* determined by using the < operator, lexicographically precedes the
* other string. In this case, <code>compareTo</code> returns the
* difference of the two character values at position <code>k</code> in
* the two string -- that is, the value:
* <blockquote><pre>
* this.charAt(k)-anotherString.charAt(k)
* </pre></blockquote>
* If there is no index position at which they differ, then the shorter
* string lexicographically precedes the longer string. In this case,
* <code>compareTo</code> returns the difference of the lengths of the
* strings -- that is, the value:
* <blockquote><pre>
* this.length()-anotherString.length()
* </pre></blockquote>
*
* @param anotherString the <code>String</code> to be compared.
* @return the value <code>0</code> if the argument string is equal to
* this string; a value less than <code>0</code> if this string
* is lexicographically less than the string argument; and a
* value greater than <code>0</code> if this string is
* lexicographically greater than the string argument.
*/
public int compareTo(String anotherString) {
int len1 = count;
int len2 = anotherString.count;
int n = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;

if (i == j) {
int k = i;
int lim = n + i;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
} else {
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;
}




要明白就自己看看源代码
healer_kx 2010-03-23
  • 打赏
  • 举报
回复
顺便,绝对不能用 大于号和小于号。
poFate 2010-03-23
  • 打赏
  • 举报
回复
先从字符串的第一个扫描 比较大小
其实就是比较字符的大小
healer_kx 2010-03-23
  • 打赏
  • 举报
回复
Java的字符串比较大小,只能用compareTo

返回 -1 0 1
分别表示小于 等于 大于。
yang_zhi 2010-03-23
  • 打赏
  • 举报
回复
我刚试了好像不行啊,能给讲解一下吗.
奔跑哥 2010-03-23
  • 打赏
  • 举报
回复
没,string的 compareTo就是比较字母的,你搞错了。
lliiqiang 2010-03-23
  • 打赏
  • 举报
回复
是用于比较字符串的
gmh521 2010-03-23
  • 打赏
  • 举报
回复
没有现成的方法,只能自己写,思路:截取字符串然后比较。
bbb332 2010-03-23
  • 打赏
  • 举报
回复
public int compareTo(String anotherString)按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值。将此 String 对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按字典顺序此 String 对象在参数字符串之前,则比较结果为一个负整数。如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0;compareTo 只有在方法 equals(Object) 返回 true 时才返回 0。
zyus1987 2010-03-23
  • 打赏
  • 举报
回复


public class JavaTest {
String str1 = "abcdefg";
String str2 = "gfedcba";
public static void main(String args[]){
JavaTest javaTest = new JavaTest();
int flag = javaTest.str1.compareTo(javaTest.str2);
if(flag==0){
System.out.println("str1 = str2"+"----str1.compareTo(str2) = "+flag);
}else if(flag>0){
System.out.println("str1 > str2"+"----str1.compareTo(str2) = "+flag);
}else{
System.out.println("str1 < str2"+"----str1.compareTo(str2) = "+flag);//被执行,打印str1 < str2----str1.compareTo(str2) = -6
}
}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧