java 字符串数组 字符串 比较

如题所述

比较的规则和数据库中的order by效果一致
实现代码如下
/**
* Name: 比较两个字符串大小
* null自动转为空,空字符串最大;
*
* @param first 要比较的第一个字符串;
* second 要比较的第二个字符串;
* @return first大于second返回正数;
* first等于second返回0;
* first小于second返回负数;
* 内部异常默认返回0;
* 返回值非固定值;
*/
public static int compareString(String first,String second){
int result = 0;

try{
//null转空
first = first==null?"":first;
second = second==null?"":second;

//预先记录字符串长度,避免反复读取
int firstLength=first.length();
int secondLength=second.length();

//处理含有空串的特殊情况
if("".equals(first) || "".equals(second)){
//谁长谁小
result = secondLength-firstLength;
}else{
//临时空间,用来存放ascii码总和
int firstCount = 0;
int secondCount = 0;
//用纯运算得出两个数中较小的数,实在是bt

int minLength = (secondLength*(firstLength/secondLength) +
firstLength*(secondLength/firstLength))/(firstLength/secondLength +
secondLength/firstLength);
//按两个字符串中较短的位数去逐位截取,防止越界
for(int i=0;i<minLength;i++){
//求ascii码和
firstCount+=first.substring(i,i+1).getBytes()[0];
secondCount+=second.substring(i,i+1).getBytes()[0];
//和不相等,说明已经比较出了大小
if(firstCount!=secondCount){
break;
}
}

if(firstCount==secondCount){
//长度长的大
result = firstLength-secondLength;
}else{
//总和大的大
result = firstCount-secondCount;
}
}
}catch (Exception e) {}

return result;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-04
1. C 语言
  在C语言中字符串和字符数组基本上没有区别,都需要结束符;如:char s[4]={'a','b','c','d'};此字符数组的定义编译可以通过,但却没有关闭数组,若其后需要申请内存,那么以后的数据均会放入其中,尽管它的长度不够,但若为 char s[5]={'a','b','c','d'};则系统会自动在字符串的最后存放一个结束符,并关闭数组,说明字符数组是有结束符的;
而字符串定义的长度必须大于字符序列的长度,如:char s1[4]={"abcd"};编译不能通过,而应写成char s1[5]={"abcd"};并且系统会自动在字符串的最后存放一个结束符,说明字符串有结束符;
在C语言中使用strlen()函数可以测数组的长度,strlen()函数计算的时候不包含结束符'/0'。
char s[5]={'a','b','c','d'};
char s1[5]={"abcd"};
int a=strlen(s);
int b=strlen(s1);
结果是a,b均为4;

2.Java语言
  字符串和字符串数组都是不需要结束符的;
  如:char[] value={'j','a','v','a','语','言'};
String s1=new String(value);
String s2="java语言";  
  int a=value.length;
int b=s1.length();
int c=s2.length();
  运行得到的结果a,b,c都是6,说明字符串和字符串数组都不需要结束符。但注意此处value.length和s1.length(),在数组中有名常量length可以记录数组对象的长度,而length()是File类中的一个实例方法,用于返回文件的大小,当然也可以返回字符串的大小。
第2个回答  2012-05-31
//<这个才是数组值与值的比较,注:我前面导入了Array包。> System.out.字符串是一个比较特殊的对象。字符串之所以特殊,是因为java会在内容中为r
第3个回答  2012-06-07
看看比较的时内容还是比较的是内存地址,比较这玩意的时候,也要看看有没有重写equals()方法。
第4个回答  推荐于2016-06-09
//字符串的比较
public class StringCompare {
public static void main(String args[]) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println("s1==s2 ?: " + (s1 == s2));
System.out.println("s1==s3 ?: " + (s1 == s3));
System.out.println("s3==s4 ?: " + (s3 == s4));
System.out.println("s1.equals(s2) ?: " + s1.equals(s2));
System.out.println("s1.equals(s3) ?: " + s1.equals(s3));
System.out.println("s3.equals(s4) ?: " + s3.equals(s4));
System.out.println("s1.compareTo(s3) ?: " + s1.compareTo(s3));
String ss1= String.valueOf(123); //s1内容为:"123"
String ss2= String.valueOf(false); //s2内容为:"false"
String ss3= String.valueOf(4.5f); //s3内容为:"4.5"
String ss4= ss1+ss2+ss3; //s4内容为:"123false4.5"
System.out.println(ss4);
}
}本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网