佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 711|回复: 5

请问有关java 的String的问题

[复制链接]
发表于 20-2-2006 09:00 AM | 显示全部楼层 |阅读模式
要怎样比较String的大小?有特别的keyword吗?

像,
String a,b;

if(a > b)
   System.out.println(b);
   System.out.println(a);
else
   System.out.println(a);
   System.out.println(b);

还有java有toUpper()之类的keyword吗?
我想做一个选择的statement可是不行.

像,

char choice="n";

do{
   ........//get choice
}while(choice=="y"||choice=="Y")//error

[ 本帖最后由 城之内 于 20-2-2006 09:01 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

我是耕田的 该用户已被删除
发表于 20-2-2006 02:03 PM | 显示全部楼层

要怎样比较String的大小?有特别的keyword吗?

Normally, we use compareTo to compare 2 strings
For example,

String one = "aa";
String two = "ab";

System.out.println("one.compareTo(two): " + one.compareTo(two));
System.out.println("one.compareTo(one): " + one.compareTo(one));
System.out.println("two.compareTo(one): " + two.compareTo(one));

String and char are different actually
String got a function called toUpper(), while char dont have
we represent String using "s", while represent char as 's'

Lets said
String choice = "n";

while ("Y".equals(choice.toUpper()) {
}
回复

使用道具 举报

 楼主| 发表于 20-2-2006 05:16 PM | 显示全部楼层
原帖由 我是耕田的 于 20-2-2006 02:03 PM 发表
Normally, we use compareTo to compare 2 strings
For example,

String one = "aa";
String two = "ab";

System.out.println("one.compareTo(two): " + one.compareTo(t ...


好仔细哦!谢谢你!!!你人真好!
回复

使用道具 举报

 楼主| 发表于 21-2-2006 05:15 PM | 显示全部楼层
请问java有clear screen的吗?
回复

使用道具 举报

发表于 21-2-2006 07:07 PM | 显示全部楼层
你可以用 compareToIgnoreCase() method
例如
        String str1="Apa MacAm";
        String str2="aPa Macam";
        
        System.out.println(str1.equalsIgnoreCase(str2)); //true;

如果你有用==(shallow comparison)在于object做比较的时候,有一点需要注意,
例如
        String temp1 = "a";
        String temp2 = "a";
        
        System.out.println(temp1 == temp2);//true
        
        String aa = "a";
        String bb = new String("a");
        
        System.out.println(aa == bb);//false

char 是primitive type当然没有象String 有toUpperCase()之内的helper methods,
它的wrapper class 是有的.
例如
        char chr = "a";
        
        System.out.println(Character.toUpperCase(chr)); //A

据我所知,java是没有clear screen,但你可以用一种笨方法骗过去,
例如

        int clrLine = 20;
        
        for (int i = 0; i < clrLine; i++) {
            System.out.println();
        }
回复

使用道具 举报

 楼主| 发表于 22-2-2006 09:07 AM | 显示全部楼层
原帖由 黑木头 于 21-2-2006 07:07 PM 发表
你可以用 compareToIgnoreCase() method
例如
        String str1="Apa MacAm";
        String str2="aPa Macam";
        
        System ...


谢谢你!!现在很难遇到像你们那么好的人了!
回复

使用道具 举报

Follow Us
您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 21-9-2024 10:37 PM , Processed in 0.108508 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表