java 6 17 32_java学习class6(1)

一.“级联”调用

1.程序源代码

1 packageclass6;2

3 public classMyCounter {4

5 private int password = -1;6

7

8 //调用本类(1)

9 publicMyCounter transfer1()10 {11 MyCounter test = newMyCounter();12 test.password = 1;13 System.out.println("调用一执行了"+"将password变成"+test.password);14 returntest;15

16 }17

18 //调用本类(2)

19 public MyCounter transfer2(intpassword)20 {21 this.password =password;22 System.out.println("调用二执行了"+"将password变成"+this.password);23 return this;24 }25

26 public static voidmain(String[] args) {27

28 MyCounter test = newMyCounter();29 System.out.println("密码的初始值是:"+test.password);30 test.transfer1().transfer2(2).transfer2(3);31 }32

33 }

2.结果截图

e64d41d4d4e7c09f387b9cecbf316ded.png

二.String类的常见函数

1.Length():用一个String类的对象来调用,返回这个字符串的长度.

eg.String str;

int length = str.Length();//此时的length就是该字符串的长度

2.charAt():返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推。

声明:public void charAt(index);其中index指的是索引值.

eg.

1 packagecom.yiibai;2

3 import java.lang.*;4

5 public classStringDemo {6

7 public static voidmain(String[] args) {8

9 String str = "This is yiibai";10

11 //prints character at 1st location

12 System.out.println(str.charAt(0));13

14 //prints character at 5th location i.e white-space character

15 System.out.println(str.charAt(4));16

17 //prints character at 18th location

18 System.out.println(str.charAt(17));19 }20 }

3. getChars():截取字符串中指定长度,指定起始位置的一段字符声明:void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)

sourceStart指定了子串开始字符的下标,sourceEnd指定了子串结束后的下一个字符的下标。

因此,子串包含从sourceStart到sourceEnd-1的字符。接收字符的数组由target指定,target中开始复制子串的下标值是targetStart。

eg.

1 String str = "abcdefghikl";2 Char[] ch = new char[8];3 str.getChars(2,5,ch,0);

4.replace():方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。

声明:StringObject.replace(regexp/substr,replacement)

regexp/substr规定了子字符串或要替换的模式的 RegExp 对象。如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。

replacement规定了替换文本或生成替换文本的函数。

eg.

1 String s = "I Love Java!";2 String r = "you";3 s = s.replace("I",r);4 System.out.println(s);

5. toUpperCase():方法用于把字符串转换为大写。

eg.

String s ="abcd";

System.out.printfln("未修改前的字符串是:"+s);

s=s.toUpperCase();

System.out.printfln("修改后的字符串是:"+s);

6. toLowerCase(): toUpperCase():方法用于把字符串转换为小写。

eg.

String s ="ABCD";

System.out.printfln("未修改前的字符串是:"+s);

s =s.toUpperCase();

System.out.printfln("修改后的字符串是:"+s);

7.trim():去掉字符串首尾的空格。

eg.

1 public static voidmain(String arg[])2 {3

4 String a=" I Love Java ";5

6 String b="I Love Java";7

8 System.out.println(b.equals(a)); //false

9

10 a=a.trim();//去掉字符串首尾的空格

11

12 System.out.println(a.equals(b)); //true

13 }

8.toCharArray():该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符

eg.

1 public classclass6_32

3 {4 public static voidmain(String args[])5 {6 String str="I Love Java";7

8 char[] c=str.toCharArray();9

10 System.out.println("数组c的长度为:"+c.length);//11

11

12 System.out.println(c);//I Love Java

13

14 }15

16 }

三.String.equals()方法源码

1 1.*@param   obj   the reference object with which to compare.2 2.    * @return  {@code true} if thisobject is the same as the obj3 3.    *          argument; {@code false} otherwise.4 4.    *@see     #hashCode()5 5.    *@see     java.util.HashMap6 6.    */7 7.   public booleanequals(Object obj) {8 8.       return (this ==obj);9 9.   }10 1./**11 2.     * Compares this string to the specified object.  The result is {@code12 3.     * true} if and only if the argument is not {@codenull} and is a {@code13 4.     * String} object that represents the same sequence of characters as this14 5.     * object.15 6.     *16 7.     *@paramanObject17 8.     *         The object to compare this {@codeString} against18 9.     *19 10.     *@return{@codetrue} if the given object represents a {@codeString}20 11.     *          equivalent to this string, {@codefalse} otherwise21 12.     *22 13.     *@see#compareTo(String)23 14.     *@see#equalsIgnoreCase(String)24 15.*/25 16.    public booleanequals(Object anObject) {26 17.        if (this ==anObject) {27 18.            return true;28 19.        }29 20.        if (anObject instanceofString) {30 21.            String anotherString =(String) anObject;31 22.            int n =value.length;32 23.            if (n ==anotherString.value.length) {33 24.                char v1[] =value;34 25.                char v2[] =anotherString.value;35 26.                int i = 0;36 27.                while (n-- != 0) {37 28.                    if (v1[i] !=v2[i])38 29.                            return false;39 30.                    i++;40 31.                }41 32.                return true;42 33.            }43 34.        }44 35.        return false;45 36.    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值