13_字符串

百知教育 - 孙帅 - 13_字符串

01_String的封装

  • 字符串是字符数组封装而成的
  • 代码:
    package day14;
    public class TestString{
     public static void main(String[] args){
      char[] cs = {'h','e','l','l','o','w','o','r','l','d'};
      System.out.println(cs);
      
      MyString ms = new MyString(cs);
      ms.toUpperCase();
      System.out.println(ms);
      
      String s = "helloworld";
      String s2 = s.toUpperCase();
      System.out.println(s2);
     }
    }
    class MyString{
     private char[] cs;
     public MyString(char[] cs){
      this.cs = cs;
     }
     public void toUpperCase(){
      for(int i = 0; i < cs.length; i++){
       if(cs[i] >= 'a' && cs[i] <='z') cs[i] -= 32;
      }
     }
     public String toString(){
      String s = "";
      for(int i = 0; i < cs.length; i++){
       s += cs[i];
      }
      return s;
     }
    }
  • 运行结果:
    在这里插入图片描述

02_String的方法

  • 与字符数组相关的方法
    1. toCharArray() 转成字符数组
    2. new String(char[] cs) 利用字符数组构造字符串
    3. toUpperCase()/toLowerCase() 大小写转换
  • 与子串相关
    1. contains(String s) 判断s子串是否存在
    2. startsWith(String s) 判断字符串是否以s子串开头
    3. endsWith(String s) 判断字符串是否以s子串结尾
    4. indexOf(String s) 返回s子串最早出现的下标
    5. lastIndexOf(String s) 返回s子串最后出现的下标
    6. replace(String s1, String s2) 将s1子串替换为s2子串
    7. substring(int a, int b) 返回字符串中a下标到b下标的子串(包含a下标,不包含b下标)
    8. length() 返回字符串的长度
    9. split(String s) 将字符串以s为分隔符,拆分为多个子串
    10. trim() 去除字符串前后的回车与空格
  • 代码:
    package day14;
    public class TestString1 {
     public static void main(String[] args){
      //String s = "abcdefghijklmnopqrstuvwxyz";
      
      /*
      char[] cs = s.toCharArray();
      for(int i = 0; i < cs.length; i++){
       if(cs[i]>='a' && cs[i]<='z') cs[i] -= 32;
      }
      String s2 = new String(cs);
      System.out.println(s2);
      */
      
      /*
      String s1 = "ABC";
      String s2 = "ABD";
      System.out.println(s1.compareTo(s2));
      */
      String s = "ab:nd:er:thsu:we";
      String[] ss = s.split(":");
      for(int i = 0; i < ss.length; i++){
       System.out.println(ss[i]);
      }
      s = "           abc         ";
      System.out.println(s.trim());
     }
    }
  • 运行结果:

    在这里插入图片描述

03_String不可变

  • 字符串的内容不可变的作用

    我们可以利用方法区里的串池去共享字符串对象

  • 代码:
    package day14;
    public class TestStringAppend{
     public static void main(String[] args){
      String s1 = "abc";
      String s2 = "abc";
      System.out.println(s1 == s2);
      s1 += "d";
      System.out.println(s1);
      System.out.println(s2);
      System.out.println(s1 == s2);
      
      String s3 = new String("abc");
      String s4 = new String("abc");
      System.out.println(s3 == s4);
     }
    }
  • 运行结果:

    在这里插入图片描述

04_String累加

  • 弊端:
    String字符串连接时会产生大量的中间对象
  • 解决方案:
    使用StringBuilder创建对象,之后再调用toString()方法返回字符串。
  • System.nanoTime()方法
    返回系统时间(long)
  • 代码:
    package day14;
    public class TestStringAppend{
     public static void main(String[] args){
      /*
      String s1 = "abc";
      String s2 = "abc";
      System.out.println(s1 == s2);
      s1 += "d";
      System.out.println(s1);
      System.out.println(s2);
      System.out.println(s1 == s2);
      
      String s3 = new String("abc");
      String s4 = new String("abc");
      System.out.println(s3 == s4);
      */
      
      System.out.println(stringBuilderAppend());
      System.out.println(stringAppend());
     }
     static long stringAppend(){
      long t1 = System.nanoTime();
      String s = "";
      for(int i = 1; i <= 100000; i++){
       s = s +"A";
      }
      long t2 = System.nanoTime();
      return t2 - t1;
     }
     static long stringBuilderAppend(){
      long t1 = System.nanoTime();
      String s = "";
      StringBuilder sb = new StringBuilder(s);
      for(int i = 1; i <= 100000; i++){
       sb.append("A");
      }
      s = sb.toString();
      long t2 = System.nanoTime();
      return t2 - t1;
     }
    }
  • 运行结果:
    在这里插入图片描述
  • 代码(面试题):
    package day14;
    public class TestExam{
     public static void main(String[] args){
      long t1 = System.nanoTime();
      String s = "a"+"b"+"c"+"d"+"e"+"f";
      long t2 = System.nanoTime();
      System.out.println(t2-t1);
      
      long t3 = System.nanoTime();
      String s2 = "a";
      StringBuilder sb = new StringBuilder(s2);
      sb.append("b").append("c").append("d").append("e").append("f");
      s2 = sb.toString();
      long t4 = System.nanoTime();
      System.out.println(t4-t3);
     }
    }  // 与上题相比,此题String字符串直接累加速度反而快。因为在编译器有初级的运算功能,能直接计算出字符串字面值累加的结果. "a"+s 则算不出来.
  • 运行结果:
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值