一、String

1.1 String是不可变对象

  • String的底层是一个 char类型字符数组
  • String类是final修饰的,不能被继承,不能改变,但引用可以重新赋值
  • String采用的编码方式是Unicode方式
  • 通过 new 创建的字符串对象,每一次new都会申请一个内存空间,虽然内容相同,但是地址值不同;
  • 以“”方式给出的字符串,只要字符序列相同(顺序和大小),无论在程序代码中出现几次,JVM都会建立一个 String 对象,并在 字符串池 中维护。
  • String是一个引用类型

1.2 String常量池

  • java为了提高性能,静态字符串在 常量池 中创建并且是同一对象,使用时 直接拿去用;
  • 对于重复的字符串直接量会在jvm会去常量池中查找,如果有则返回,无则创建
public class string01 {
public static void main(String[] args) {
//在常量池中查找,没有创建一个
String s="Hello";
//在常量池中查找,有的话不会创建新对象
String s1="Hello";
System.out.println(s==s1);//true
String s2=new String("Hello");//新建一个对象
System.out.println(s1==s2);//false
}
}
  • String s= new String("a");创建了几个对象?
  • 两个或一个,如果常量池中有a则返回总共1个,如果没有则创建a,加上new关键字是2个

1.3 String构造方法

package cn.tedu.day02;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.io.UnsupportedEncodingException;

public class String02 {

    public static void main(String[] args) throws UnsupportedEncodingException {
/*
* String的构造方法:1、无参构造*/
String s=new String();
System.out.println(s);
/*
* utf-8*/
String s1=new String(new byte[]{'1','2','3','4'},1,3,"utf8");
System.out.println(s1);
String s2=new String(new char[]{'H','e','l','l','o'});
System.out.println(s2);
String s3=new String(new char[]{'A','B','C'},1,2);
System.out.println(s3);
String s4=new String(new int[]{'1','2','3'},1,2);
System.out.println(s4);
String s5=new String(new StringBuffer("high"));
System.out.println(s5);
String s6=new String(new StringBuilder("agh"));
System.out.println(s6);
}
}
 

1.4.1 String的判断方法

boolean equals(Object obj):比较字符串的内容是否相同

boolean equalsIgnoreCase(String str): 比较字符串的 内容 是否 相同,忽略大小写

boolean startsWith(String str): 判断 字符串对象 是否 以指定的 str 开头

boolean endsWith(String str): 判断 字符串对象 是否 以指定的 str 结尾

package cn.tedu.day02;

public class String03 {
public static void main(String[] args) {
String s="abc";
String s1="Aac";
String s2=new String("abc");
/*第一种比较的方式*/
System.out.println(s==s2);//f 比较的对象,比较的地址值
System.out.println(s.equals(s2));
//2、不区分大小写
System.out.println(s.equalsIgnoreCase(s1));
//3、判断以什么开始
System.out.println(s.startsWith("ab"));
//4、判断以什么结束
System.out.println(s.endsWith("c"));
} }

1.4.2 String的截取

  • int length(): 获取 字符串 的 长度,其实也就是字符个数
  • char charAt(int index): 获取 指定 索引处 的 字符
  • int indexOf(String str): 获取 str 在字符串 对象 中第一次出现的索引
  • String substring(int start): 从 start 开始 截取 字符串
  • String substring(int start,int end): 从 start 开始,到 end 结束 截取字符串。前包后不包
public class String04 {
public static void main(String[] args) {
String s="Hello";
String s1="liyimeng";
String s2=new String(new char[]{1,2,3});
System.out.println(s.length());//5
System.out.println(s1.length());//8
System.out.println(s2.length());//3
//第一种;charAt:根据索引找字符串
System.out.println(s.charAt(2));//h
//第二种:indexOf:根据字符串找索引
System.out.println(s.indexOf("H"));//0
System.out.println(s1.indexOf('y',2));//2
//第三种:substring
System.out.println(s.substring(1));//ello
//第四种:substring(前包后不包)字符串没变,创建了一个新串
System.out.println(s.substring(1,4));//ell

1.4.3 String的转换

  • char[] toCharArray():把字符串转换为字符数组
  • String toLowerCase():把字符串转换为小写字符串
  • String toUpperCase():把字符串转换为大写字符串
import java.util.Arrays;
import java.util.Locale; public class String05 {
public static void main(String[] args) {
String s="l y m";
//1、toCharArray把字符串转化为字符数组
char c[]=s.toCharArray();
System.out.println(Arrays.toString(c));
c=new char[]{'H','e','l','l','o'};
String s1=new String(c);
System.out.println(s1);
//2、toUpperCase全部变大写
System.out.println(s.toUpperCase(Locale.ROOT));
//3、toLowerCase全部变小写
System.out.println(s.toLowerCase(Locale.ROOT));
}
}

1.4.4 其他方法

  • String trim(): 去除 字符串 两端空格
  • String[] split(String str): 按照 指定符号 分割 字符串
import java.util.Arrays;

public class String06 {
public static void main(String[] args) {
String s=" hello world ";
//trim:去除字符串两端空格(中间的不去)字符串不可变
System.out.println(s.trim());
//split:分割字符串
System.out.println(s.length());
System.out.println(Arrays.toString(s.split("h")));
System.out.println(s.length());
String s2="www.baidu.com";
//1、获取两个点的索引
int start=s2.indexOf(".");
int end=s2.lastIndexOf(".");
String result=s2.substring(start+1,end);
System.out.println((result));
//String api中的方法进行测试
}
}

1.5 StringBuilder

1.5.1 可变字符串

  • StringBuilder 是 一个线程 非安全 的 可变字符序列
  • StringBuilder 底层 在其父类中 封装了 一个 char类型 的数组
  • StringBuilder 创建了一个 初始容量 为 16
//构造方法
public class StringBuilder01
public class StringBuilder01 {
public static void main(String[] args) {
//构造方法
StringBuilder sb=new StringBuilder();
System.out.println("无参构造方法:"+sb);
System.out.println("初始容量:"+sb.capacity());
//
StringBuilder sb1=new StringBuilder(32);
System.out.println("自定义容量:"+sb1.capacity());
//
StringBuilder sb2=new StringBuilder("hello");
System.out.println("容量:"+sb2.capacity());
System.out.println(sb2);
}
}
public static void main(String[] args) {
//构造方法
StringBuilder sb=new StringBuilder();
System.out.println("无参构造方法:"+ sb);
System.out.println("初始容量:"+sb.capacity());
StringBuilder sb1=new StringBuilder(32);
System.out.println("自定义容量:"+sb1.capacity());
StringBuilder sb2=new StringBuilder("hello");
System.out.println("容量:"+sb2.capacity());
System.out.println(sb2);
}
}

1.5.2 常用方法

  • append(任意内容):追加字符串,数组放满,容量翻倍

  • 追加扩容问题:

    • 扩容实现原理:数组的 arrays.copyOf() 方法

• 扩容算法: 原数组容量 * 2 + 2

• 测试:

public class String02 {
public static void main(String[] args) {
StringBuilder stringBuilder=new StringBuilder("hello");
System.out.println(stringBuilder. append("lllllllllllllllllllll"));
System.out.println("sb的容量:"+stringBuilder.capacity());
System.out.println("sb的内容:"+stringBuilder);
System.out.println("sb的长度:"+stringBuilder.length());
System.out.println(stringBuilder. append("lllllllllllllllllllllllll"));//超出的字符串长度*2+2
System.out.println("sb的容量:"+stringBuilder.capacity());
System.out.println("sb的内容:"+stringBuilder);
System.out.println("sb的长度:"+stringBuilder.length());
}
}
  • insert(int ,任意类型):插入
  • delete(int start,int end):删除
  • replace(int start,int end,String s):替换
  • reverse():反转
public class StringBuilder04 {
public static void main(String[] args) {
//insert:
StringBuffer stringBuffer=new StringBuffer("hello");
stringBuffer.insert(2,"lym");
System.out.println(stringBuffer);
//delete:
StringBuffer stringBuffer1=new StringBuffer("lym");
System.out.println(stringBuffer1.delete(1,3));
//replace:
StringBuffer stringBuffer2=new StringBuffer("zhangsan");
System.out.println(stringBuffer2.replace(1,5,"qqq"));
//reverse:反转
System.out.println(stringBuffer.reverse());
} }
  • 方法返回值问题

    • 因为这些 方法 的 返回值语句 是 return this; 因此 可以 连续调用
public class String05 {
public static void main(String[] args) {
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("hello").append("lym").append(1).replace(2,5,"zhang").reverse();
System.out.println(stringBuilder);
}
}

1.5.4 StringBuilder和StringBuffer的区别:

  • StringBuffer 是 线程安全 的

• StringBuilder是 线程非安全 的,因此 运行速率 较快

1.5.5 StringBuilder和String相互转化:

1、StringBuilder转换为String

public String toString(): 通过 toString()就可以实现把StringBuilder转换为String

2、String转换为StringBuilder

pubilc StringBuilder(String s): 通过 构造方法 就可以实现String转换为StringBuilder

最新文章

  1. 用GitHub Pages搭了个静态博客
  2. JS Json数据转换
  3. [学习笔记]坚果云网盘,SVN异地代码管理
  4. 第五章GPIO接口
  5. Python之路【目录】 2
  6. 如何设置WIN10任务栏
  7. ubuntu安装jdk-6u45-linux-x64-rpm.bin
  8. sql server行级锁,排它锁,共享锁的使用
  9. MongoDB增删改查
  10. Leetcode#140 Word Break II
  11. trie树--详解
  12. js个人笔记
  13. get 和 post的使用.
  14. CSS3 @font-face详细用法(转)
  15. HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧
  16. 购物篮模型&Apriori算法
  17. ThinkPHP的使用
  18. 数据结构-快速排序(C#实现)
  19. 【Atcoder hbpc C 183】1=0.999...
  20. 安装istio v1.0 详细过程和步骤

热门文章

  1. IDEA windows版本快捷键
  2. Microsoft Office 代码执行漏洞临时防范方法
  3. Redis - 为什么 Redis 是单线程的?
  4. Windows-VS2017创建.NET项目
  5. UiPath录制器的介绍和使用
  6. js与java encodeURI 进行编码与解码
  7. 论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
  8. 解决Anaconda出现Solving environment:failed问题之一
  9. 记一次重复造轮子(Obsidian 插件设置说明汉化)
  10. 2020 CSP-J 初赛解析