Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » Java与OOP初步  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 初学 java 贴个 比较两个字符串的方法
chaostring





发贴: 6
积分: 0
于 2007-11-15 19:57 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
/*
* This class is to compare two strings.
*/

package comparestring;

/**
* This class contains a method to compare two strings
* @author Chaotring
*/
public class CompareString {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String s1 = "wel";
String s2 = "wel";
System.out.println(compStrings(s1,s2));

}

public static boolean compStrings(String str1, String str2){

/* use equals() */
//return str1.equals(str2)? true: false;

/* use compareTo() */
//return str1.compareTo(str2)==0? true: false;

/* use getChars() */
int l1 = str1.length();
int l2 = str2.length();
char[] c1 = new char[l1];
char[] c2 = new char[l2];
boolean big = false;

if(l1==l2){
str1.getChars(0, l1, c1, 0);
str2.getChars(0, l2, c2, 0);
for(int i = 0; i < l1; i++ ){
if(c1[i] != c2[i]){
break;
}else{
big = true;
}

}
}
return big;

}

}


chaostring edited on 2007-11-16 12:04


话题树型展开
人气 标题 作者 字数 发贴时间
10216 初学 java 贴个 比较两个字符串的方法 chaostring 1279 2007-11-15 19:57
8088 Re:初学 java 贴个 比较两个字符串的方法 JiafanZhou 128 2007-11-16 18:07
8252 Re:初学 java 贴个 比较两个字符串的方法 chaostring 160 2007-11-16 19:01
8099 Re:初学 java 贴个 比较两个字符串的方法 Cappuccino 300 2007-11-16 23:38
8754 Re:初学 java 贴个 比较两个字符串的方法 chaostring 143 2007-11-17 12:02

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923