SE01 Unit01 API文档 、 字符串基本操作

Java API

什么是API

  1. 由Java提供(Oracle, SUN), 现成的程序组件(类)。
  2. API封装了开发时候常用的功能!
    • 字符串操作
    • 数据管理(集合)
    • IO,文件访问
    • 线程管理

String API

学习String提供的那些方法?这些方法有那些功能!学会利用这些功能!

String 对象的特点

  1. String对象的数据不可改变!
  2. String类型对象封装了一个字符串数组
  3. 任何的操作也不能改变这个字符数组的内容

    String s = "123";
    String ss = s;
    s = s+"abc";
    System.out.println(s);//123abc
    System.out.println(ss);//123
    

说明: 在如上代码中改变的是字符串引用变量,但是字符串没有变!好处是字符串可以“作为”基本类型使用!

原理:

这里写图片描述

字符串常量的重用现象

Java中的字符串常量是尽肯能重复使用的!好处是节省资源(内存)

  1. 字符串字面量(直接量)在内容一样时候重用同一个String对象。
        String s1 = "123abc";
        String s2 = "123abc";
        //s1 s2 是字符串类型的引用变量
        //"123abc" 是直接量(字面量)
  1. 字符串常量也参与重用!
  2. 字面量、常量的运算结果是字符串,也重用同一个字符串
        String s4 = "123abc";
        String s5 = 123 + "abc";//1+"23abc"
        System.out.println(s4==s5);//true       
  1. 字符串变量,变量的运算结果 和 新创建的字符串对象不参与重用!!
        String name = in.nextLine();//Tom
        String s1 = "Tom and Jerry";
        String s2 = name + " and Jerry";
        System.out.println(s1==s2);//false
        String s3 = new String("Tom and Jerry");
        System.out.println(s3==s1);//false

原理:

这里写图片描述

经典题目:

    String s1 = "1"+"23"+"abc";
    String s2 = "1"+23+"abc";
    String s3 = '1'+23+"abc";
    System.out.print(s1==s2);
    System.out.print(s1==s3);
如上代码的执行结果:
A.truetrue B.truefalse C.falsetrue D.falsefalse

字符串中的字符

字符串中封装了一个字符数组,字符串中的字符就是char类型的数据。

  1. char 类型是整数, 是一个字符的Unicode编码。
  2. 16位无符号整数, 占用2个字节

案例:

    String s = "Tom and Jerry";
    //          0123456789012
    char c = s.charAt(4);
    System.out.println(c);//a
    System.out.println((int)c);//97

indexOf 方法

找出一个字符在字符串中的位置:

indexOf()
1. 如果有重复,找出左侧第一个位置
2. 如果没有找到,返回-1

案例:

    String s = "Tom and Jerry";
    int i = s.indexOf('a'); 
    System.out.println(i);//4
    i = s.indexOf('r');
    System.out.println(i);//10
    i = s.indexOf('X');
    System.out.println(i);//-1

str.indexOf(“查找字符串”, 起始位置)

    String url = "http://tedu.cn/index.html";
    int i = url.indexOf("/",7);
    System.out.println(i);

lastIndexOf

反序查找:从右到左查找,返回字符的位置

    String url = "http://tedu.cn/index.html";
    int i = url.lastIndexOf("/");//14
    System.out.println(i);//14

查找手册练习:

  1. 找到包 java.lang
  2. 找到类 String
  3. 找到方法 lastIndexOf()

substring 方法

从字符串中截取一部分作为子字符串

    url.substring(起始位置)
    //从起始位置开始到最后截取为子字符串

    String url = "http://tedu.cn/index.html";
    String filename = url.substring(15);
    // filename = index.html

    url.substring(起始位置, 结束位置)
    //从起始位置开始到结束位置截取为子字符串
    String url = "http://tedu.cn/index.html";
    //            01234567890123456
    // 包括起始不包括结束位置
    String str = url.substring(7, 14);
    String str = url.substring(7, 7+8);

trim

    String str = " \t Tom \n \r";
    String s = str.trim();

startsWith endsWith

检测一个字符串是否以指定字符串开头或结尾

    String str = "Hello World!";
    boolean b = str.startsWith("Hello");//true
    b = str.startsWith("World");//false

    b = str.endsWith("World");//false
    b = str.endsWith("!");//true

案例:

    String name = "demo.JPG";
    if(name.toLowerCase().endsWith(".jpg")){
        System.out.println("图片文件");
    }

StringBuilder

Java 提供的用于计算字符串的API, 其运算性能好:

案例:

    String s = "A";
    s = s + "1";
    s = s + "1";
    s = s + "1";
    System.out.println(s);

原理:

这里写图片描述

性能比较:

        String s = "A";
        long t1 = System.currentTimeMillis();
        for(int i=0; i<10000; i++){
            s = s+"1";
        }
        long t2 = System.currentTimeMillis();
        System.out.println(s.length());
        System.out.println(t2-t1);

        StringBuilder ss = 
            new StringBuilder("A");
        t1=System.nanoTime();
        for(int i=0; i<10000; i++){
            ss.append("1");
        }
        t2 = System.nanoTime();
        System.out.println(ss.length());
        System.out.println(t2-t1);

StringBuilder API:

        StringBuilder buf=
                new StringBuilder();
        buf.append("李洪鹤老师...");
        buf.insert(0, "那一年");
        buf.replace(4, 4+2, "某人");
        buf.delete(6, 6+2);
        String s = buf.toString();
        System.out.println(s); 

String 和 StringBuilder

String是不变字符串: 对象不可改变,对象中的字符数组中的数据不可改变。

StringBuilder是可变字符串:对象封装的字符数组中的数据可以改变。

StringBuilder类型的操作性能好于Srting,字符串操作建议使用StringBuilder。 字符串显示使用String。

运行期间字符串连接计算利用StringBuilder的append完成。

案例:

String s = "123";
String ss = s + "abc";
//ss = new StringBuilder(s)
//      .append("abc").toString();

案例:

String s = "123"+"456"+"abc";
String ss = "123";
String str = ss + "456"+"abc";
//在一个表达式中出现连续的字符串连接,Java会
//自动的优化为一个StringBuilder对象
//String str = new StringBuilder(ss)
//  .append("456").append("abc").toString();

在工作中一个表达式中的连续字符串连接不需要优化为StringBuilder

在反复进行字符串连接时候建议使用StringBuilder

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值