Topic: 如何比较2个字符

  Print this page

1.如何比较2个字符 Copy to clipboard
Posted by: flywithmoon
Posted on: 2004-09-09 16:39

要比较单个的字符,不是字符串数组中的某个元素,程序该怎么写?
请教了。
import java.text.*;
import java.awt.*;
import java.awt.event.*;

class test
{
String s1="abc";
String s2="ABC";
char c1,c2;
Collator myCollator = Collator.getInstance();
public static void main(String args[])
{
c1=s1.charAt(1);
c2=s2.charAt(1);
if(myCollator.compare(c1,c2)==0)
System.out.println("c1=c2");
}
}

2.Re:如何比较2个字符 [Re: flywithmoon] Copy to clipboard
Posted by: scud
Posted on: 2004-09-09 17:50

substr ?

3.Re:如何比较2个字符 [Re: flywithmoon] Copy to clipboard
Posted by: flywithmoon
Posted on: 2004-09-10 13:53

除了下面的方法还有什么哪
if(c1==c2)
或者是
if(c1.equal(c2))

4.Re:如何比较2个字符 [Re: flywithmoon] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-09-10 13:58

没有了吧

5.Re:如何比较2个字符 [Re: flywithmoon] Copy to clipboard
Posted by: heaven
Posted on: 2004-09-10 22:32

char是基本类型,不是对象,直接“==”即可,简单又实用,equals的那种代码我没有试验过,也许tiger里面的自动装包机制才支持。

6.Re:如何比较2个字符 [Re: flywithmoon] Copy to clipboard
Posted by: alpacino
Posted on: 2004-09-21 16:59

上面说的对,“==”对int,long ,char,float等基本数据类型的比较就是比较“值”,对象的比较用“==”是比较“引用”,那才需要“equals()”


   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