学会查询API
public class IndexOfDemo05 {
public static void main(String[] args) {
/**
* indexOf(字符, 位置)
* 从指定位置开始查找字符
*/
//跳过 "http://" 查找 / 的位置
String url="http://tedu.cn/index.html";
// 0123456789012345678901234
int n = url.indexOf("/", 7);
System.out.println(n);
/**
* lastIndexOf()从后向前查找指定字符串
* (字符)找到第一个就返回这个字符串的位置
* last 最后的
*/
n = url.lastIndexOf(".");
System.out.println(n);
}
}
------------------------------------------------------------------------
包含起始位置,不包含结束位置
package se1.day01;
public class SubstringDemo06 {
public static void main(String[] args) {
/**
* 截取一个字符串的部分作为子字符串
*/
String url="http://tedu.cn/index.html";
//
0123456789012345678901234
String host = url.substring(7, 14);
System.out.println(host);//tedu.cn
//substring支持按照长度截取字符串
//在url中从7位置开始截取4个字符
String name=url.substring(7, 7+4);
System.out.println(name);//tedu
/**
* 业务案例
* 从URL中截取 主机(host)地址
*
* http://tmooc.cn/index.html
* http://tedu.cn/index.html
* http://canglaoshi.org/day01/index.html
*/
url="http://canglaoshi.org/index.html";
int i=url.indexOf("/",7);//从第7个位置开始,往后检查第一个/的位置,返回值是整数
host = url.substring(7, i);//从第7个位置开始,截取到第一个/
System.out.println(host);
/**
* substring(起始位置)
*/
String str="18岁给我一个姑娘";
// 012 3 4 5 6 7 8
String s = str.substring(7);
System.out.println(s);
/**
* 从URL中截取访问的文件名
*/
i=url.lastIndexOf("/");
String file = url.substring(i+1);
System.out.println(file);
}
}
--------------------------------------------------------------------------------------------------------------------
trim-----去除字符串两边的空白(不要说空格)
package se1.day01;
public class TrimDemo07 {
public static void main(String[] args) {
/**
* trim 去除字符串两端的空白
* 空白: 包括 空格,Tab,回车,换行等
*/
String str = " \n \r Tom \t \r \n ";
String s = str.trim();//返回的是新字符串,原字符串不变
System.out.println(s);//Tom
System.out.println(s == str);//false
str = "Tom";
s = str.trim();
System.out.println(s==str);//true
//经典题目:
String s1 = " ABC ";
s1.trim();
System.out.println(s1);
//如上代码的输出结果是:
//A."ABC" B." ABC "
//C.编译错误 D.运行异常
//答案: B
}
}
-------------------------------------------------------------------------
package se1.day01;
public class EndsWithDemo08 {
public static void main(String[] args) {
/**
* 检查一个字符串是否已指定的字符串为结尾
*
*/
String file = "logo.png"; //这里可以改为键盘输入
//检查文件是否为 png 图片文件
boolean b = file.endsWith(".png");//以.png为结尾
System.out.println(b);
//如果检查空串,总是返回true
b = file.endsWith("");
System.out.println(b);
//和字符串自己比较,返回true
b = file.equals("logo.png");
System.out.println(b);
}
}
-----------------------------------------------------------------------------------------------------
StringBuilder-------------------------动态字符串
数组复制在内存中开销比较大
什么叫可变的数组?内部封装的数组可以改
char[]数组里面放了16个0,就是16个元素的char数组,我们讲过字符数组默认是0
什么叫可以改变?StringBuilder有个方法-----append
append一个字符
StringBuilder buf = new StringBuilder();
buf.append('范');
它扩不扩容我不管,它会自动的,你只要调用append
它所谓扩容呢?就是它把这个旧数组扔了,然后换成一个新数组
扩容规则:一倍+2
原先16个,一倍32+2=34
为什么一倍+2? API规定
length()对于StringBuilder来说,它是检查有效的字符个数
capacity()----返回数组的大小
buf.capacity();
----------------------------------------------------------------------------------------------------------------
package se1.day01;
public class StringBuilderDemo09 {
public static void main(String[] args) {
/**
* StringBuilder: 内容可变的字符串,简称
* 可变字符串, 与其相反 String 是内容不可
* 变的字符串, 简称不变字符串
*
* StringBuilder的优点是操作性能好
*/
StringBuilder buf=new StringBuilder();
//检查有效字符个数
System.out.println(buf.length());//0
//检查StringBuilder中的数组容量
System.out.println(buf.capacity());//16
buf.append('范');
//检查有效字符个数
System.out.println(buf.length());//1
//检查StringBuilder中的数组容量
System.out.println(buf.capacity());//16
buf.append("**");
buf.append("那一年, 在他18岁的时候");//这句有空格
//检查有效字符个数
System.out.println(buf.length());//1
//检查StringBuilder中的数组容量
System.out.println(buf.capacity());//16
buf.append("他说:我还年轻,给我一个姑娘");
//检查有效字符个数
System.out.println(buf.length());//30
//检查StringBuilder中的数组容量
System.out.println(buf.capacity());//34
System.out.println(buf);
buf.insert(3, "老湿")
buf.delete(5, 5+3)
buf.replace(1, 1+2, "**")
因为返回的是此对象---buf.insert(3, "老湿") 返回的是buf对象
//StringBuilder方法返回自身对象.
//适合连续"函数式编程"
----------------------------------------------
buf.insert(3, "老湿")
.delete(5, 5+3)//从5开始删掉3个
.replace(1, 1+2, "**")//从1开始,换2个,换成xx
.append("!")
.append("!")
.append("!");
System.out.println(buf);
}
}
String是用来做输出的.而StringBuilder是用来做计算的,那为什么专门弄StringBuilder这个类?
主要是操作性能好
--------------------------------------------------------
package se1.day01;
public class StringBuilderDemo10 {
public static void main(String[] args) {
/**
* StringBuilder的操作性能好于String
*
*/
String s1 = "A";
//s1 = s1+"A"; //数组复制
//s1 = s1+"A";//每次这么操作以后,都会多一个内存垃圾
//先创建一个对象new StringBuilder(s1)----然后调用append方法---------最后转成字符串交给s1,转完字符串以后,新创建的 StringBuilder对象就被当内存垃圾扔掉-----
//实际的计算是s1 = new StringBuilder(s1)------java在自动会这么做
// .append("A").toString();
//s1 = s1+"A";
System.out.println(s1);
test1(50000);
test2(50000);
}
public static void test1(int n){
long t1=System.nanoTime();
String s1="";
for(int i=0; i<n; i++){
s1=s1+"A"; //new StringBuilder()
}
System.out.println(s1.length());
long t1=System.nanoTime();
System.out.println(t2-t1);
}
public static void test2(int n){
long t1=System.nanoTime();
StringBuilder buf=new StringBuilder();
for(int i=0; i<n; i++){
buf.append("A");
}
System.out.println(buf.length());
long t1=System.nanoTime();
System.out.println(t2-t1);
}
}
--------------------------------------------------------------------------
package se1.day01;
public class StringBuilderDemo11 {
public static void main(String[] args) {
/**
* 何时使用StringBuilder优化String连接
*/
//1.静态字符串连接, 不需要用StringBuilder!
// 静态字符串在编译阶段编译为一个对象!
String s1 = "A" + "bcd" + "DEF";-----------------这个性能好
System.out.println(s1);
String s2 = new StringBuilder("A")
.append("bcd").append("DEF")
.toString();
System.out.println(s2);
//2. 写在一行上的字符串连接,不用StringBuilder
// Java编译器会自动将一行上的字符串连接优化为一个StringBuilder对象
//
String s3 = "ABC";
String s4 = "DEF";
String s5 = "EFG";
String ss = s3+s4+s5;
// ss = new StringBuilder(s3)
// .append(s4)
// .append(s5).toString();
//3. 多行多次操作字符串时候需要优化
//案例: 将一个整数数组连接为一个字符串
// {1, 6, 8, 9, 10} 连接为
// "[1, 6, 8, 9, 10]"
//错误的写法: 运行期间创建了多个StringBuilder
int[] arr={1, 6, 8, 9, 10};
String str="["+arr[0];
for(int i=1; i<arr.length; i++){
str = str+", "+arr[i];
}
str+="]";
System.out.println(str);
//正确的写法: 使用一个StringBuilder
StringBuilder buf=new StringBuilder("[");
buf.append(arr[0]);
for(int i=1; i<arr.length; i++){
buf.append(", ").append(arr[i]);
}
buf.append("]");
String str2 = buf.toString();
System.out.println(str2);
}
}
-------------------------------------------
上课给的案列都做熟+参考的练习