课后作业
1-1程序代码:
1-2输出结果:
1-3总结:new对象时,新开辟一个地址,所以两个new对象并不相等。
2-1源代码:
2-2输出结果:
2-3总结:用String.equals()方法 可以实现不同对象之间(除去地址的差异)的比较。
3-1 String类
(1) Length():计算字符串的长度
(2) charAt():检索字符串中的字符
(3) getChars():getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)将字符从此字符串复制到目标字符数组。
要复制的第一个字符在索引 srcBegin 处;要复制的最后一个字符在索引 srcEnd-1 处(因此要复制的字符总数是 srcEnd-srcBegin)。要复制到 dst 子数组的字符从索引 dstBegin 处开始,并结束于索引.例如
String str = "abcdefghikl";
Char[] ch = new char[8];
str.getChars(2,5,ch,0);
就是从str的第二个字母开始一直复制到第五个,一共是3个字符,从ch的第一个开始接受。
(4) replace():参数是char,也可以是字符串,实现字符和字符串的替换
(5) toUpperCase():全部字符变为大写字母,并且返回新的字符串。
(6) toLowerCase():全部字符变为小写字母,并且返回新的字符串。
(7) trim():除去字符串开头和末尾的空格或其他字符。函数执行成功时返回删除了string字符串首部和尾部空格的字符串,发生错误时返回空字符串("")。如果任何参数的值为NULL,Trim() 函数返回NULL。
(8) toCharArray():将字符串转化为字符数组。