查看: 739|回复: 5
|
请问有关java 的String的问题
[复制链接]
|
|
要怎样比较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 编辑 ] |
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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 ...
谢谢你!!现在很难遇到像你们那么好的人了! |
|
|
|
|
|
|
| |
本周最热论坛帖子
|