自学Java系列 笔记4 Java常用类 1

String

1. String 是不可变的字符序列!

 2. 关于字符串缓冲池:直接通过 = 为字符串赋值, 会先在缓冲池中查找有没有一样的字符串,若果有就把那个引用赋给字符串变量,否则, 会创建一个新的字符串, 并把对应的字符串放入到缓冲池中.

代码实例:

@Test

      publicvoid testNewString(){

           Stringstr1 = "hello world";

           Stringstr2 = "hello world";

          

           System.out.println(str1== str2);           //true

           System.out.println(str1.hashCode());

           System.out.println(str2.hashCode());

          

           Stringstr3 = new String("abcde");

           Stringstr4 = new String("abcde");

           System.out.println(str3== str4);           //false

      }

3. 字符串的几个常用方法:

3.1 去除前后空格的 trim()方法:

      publicvoid testTrim(){

           Stringstr = "    ab  cd   ";

           System.out.println("--"+ str + "--");

          

           Stringstr2 = str.trim();

           System.out.println("--"+ str + "--");

           System.out.println("--"+ str2 + "--");

      }

3.2 求子字符串的方法: subStringfromIndex:从 fromIndex开始,包含fromIndex,且String 的字索引从0开始

      publicvoid testSubString(){

           Stringstr = "http://www.atguigu.org/index.jsp?name=Tom";

          

           Stringstr1 = str.substring(7);

           System.out.println(str1); 

      }

 

3.3 subString(fromIndex,toIndex):[fromIndex,toIndex) ,(表示半开半闭)

      publicvoid testSubString(){

           Stringstr = "http://www.atguigu.org/index.jsp?name=Tom";

          

           Stringstr2 = str.substring(1, 5);

           System.out.println(str2);

      }

注意:java中的区间表示,都是半闭半开

3.4 indexOf:求指定字符的索引

通常indexOf 和subString(fromIndex,toIndex)一起来用:

    

public voidtestIndexOf(){

         String str ="http://www.atguigu.org/index.jsp?name=Tom";

        

         System.out.println(str.indexOf("//")); 

         System.out.println(str.lastIndexOf("/"));

        

         int beginIndex = str.indexOf("//")+ 2;

         int endIndex =str.lastIndexOf("/");

         System.out.println(str.substring(beginIndex,endIndex));

     }

 

3.5 split(String regex):把字符串拆分成字符串数组

public voidtestSplit(){

         String str ="aaa-bb-ccccc-dd-eee-ff-g";

         String [] values = str.split("-");

        

         for(String s: values){

              System.out.println(s);

         }

     }

 

 

3.6 equals(): 比较字符串内容是否相等必须使用该方法, 而不能直接使用 ==

 

 

 

练习:

给定一个字符串:acmfnz

经运算求每个字符都向后窜一位的字符串: bdngoa

若某个字符串已经是z,则返回到最开始的a

    

public voidtestTransforString(){

         String str = "acmfnz&Acc10ee-EMM";

         System.out.println(str);

        

         for(int i = 0; i < str.length();i++){

              char ch = str.charAt(i);

              if(ch >= 'a' && ch<= 'z'){

                   if(ch == 'z')

                       ch = 'a';

                   else

                       ch = (char)(1 + ch);

              }else if(ch >= 'A' &&ch <= 'Z'){

                   if(ch == 'Z')

                       ch = 'A';

                   else

                       ch = (char)(1 + ch);

              }

              System.out.print(ch);

         }

     }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值