String
1. 特性:
-
任意字符串都是String的对象
-
字符串的内容是不可改变的
-
相同内容的字符串地址是一样的
-
final修饰,不可被继承
-
相同的两段字符串是有缓存的,重复使用的
2. 字符串常量池的变更历史
JDK1.7之前:
运行时常量池(包括字符串常量池)存放在方法区,此时 hotspot虚拟机对方法区的实现改为永久代
JDK1.7:
字符串常量池从方法区拿到了堆中
运行时常量池剩下的东西还在方法区,也就是Hotspot的永久代
JDK1.8:
hotspot移除了永久代,用元空间代替,这时候字符串常量池还在堆,运行时常量池还在方法区,只不过方法区的实现从永久代变成元空间
3. 常用方法
序号 | 方法定义 | 类型 | 描述 |
---|---|---|---|
1 | public String(char[] value) | 构造 | 将一个字符数组变成字符串 |
2 | public String(char[] value,int offset,int count) | 构造 | 将一个指定范围的字符数组围变成字符串 |
3 | public String(byte[] bytes) | 构造 | 将一个字节数组变成字符串 |
4 | public String(byte[] bytes,int offset,int length) 构造 | 将一个指定范围的字节数组变成字符串 | |
5 | public char[] toCharArray() | 方法 | 将字符串变成字符数组 |
6 | public char charAt(int index) | 方法 | 从字符串中提取指定位置的字符 |
7 | public byte[] getBytes() | 方法 | 将字符串变成字节数组 |
8 | public int length() | 方法 | 取得字符串的长度 |
9 | public int indexOf(String str,int fromIndex) | 方法 | 从指定位置查找指定字符串的位置 |
10 | public String trim() | 方法 | 清除左右两端的空格 |
11 | public String substring(int begin,int end) | 方法 | 截取指定范围的字符串 |
12 | public String[] split(String regex) | 方法 | 按照指定的字符串对字符串进行拆分 |
13 | public String toUpperCase() | 方法 | 将 字符串全部变成大写 |
14 | public String toLowerCase() | 方法 | 将字符串全部变成小写 |
15 | public boolean startsWith(String prefix) 方法 | 判断是否以指定字符串开头 | |
16 | public boolean endWith(String suffix) | 方法 | 判断是否以指定字符串结尾 |
17 | public boolean equals(String str) | 方法 | 判断两个字符串的内容是否相同 |
18 | public boolean equalsIgnoreCase(String str) | 方法 | 忽略大小写,判断两个字符串的内容是否相同 |
19 | public String repaceAll(String regex,String replacement) | 方法 | 字符串 替换 |
3.1 字符串截取
示例
String text = "hello world";
System.out.println(text.substring(6,11));
结果:
world
3.2 按照指定的字符串拆分字符串
示例
String text = "hello world";
String[] text1 = text.split("");
for(int i= 0;i<text1.length;i++){
System.out.print(text1[i]);
}
结果:
hello world
3.3 字符串的大小写转换
示例:
String text = "hello world";
System.out.println(text.toUpperCase());
String text1 = "HELLO WORLD";
System.out.println(text1.toLowerCase());
结果:
HELLO WORLD
hello world
3.4 判断是否以指定的字符串开头或结尾
示例:
String text = "hello world";
if(text.startsWith("hello")){
System.out.println(text+"以hello开头");
}
String text1 = "HELLO WORLD";
if(text1.endsWith("WORLD")){
System.out.println(text1+"以WORLD结尾");
}
结果:
hello world以hello开头
HELLO WORLD以WORLD结尾
3.5 将一个指定的字符串替换成其他的字符串
示例:
tring text = "hello world";
System.out.println(text.replaceAll("l","x"));//将l全部替换为x
结果:
hexxo worxd
4. 字符串拼接
text1 = text1+text2+text3
使用符号"+"进行字符串拼接时,内存里首先有 text1,text2,text3 3个String对象
执行这个拼接语句后, 先拼接text1+text2并开辟一块内存存放这个值,再将这个值与text3拼接.完成该语句功能,
此时内存中有 text1原来的值, text1+text2 这两块内存垃圾.
所以尽量避免使用String字符串拼接
StringBuffer类
1. 概念
使用String类的对象进行字符串拼接时,会浪费大量的内存,而StringBuffer类是java专门用于字符串拼接的类.StringBuilder是线程不安全的实现.StringBuffer是线程安全的实现
2. 常用方法
序号 | 方法定义 | 类型 | 描述 |
---|---|---|---|
1 | public StringBuffer() | 构造 | StringBuffer的构造方法 |
2 | public StringBuffer append(char c) | 方法 | 在StringBuffer中提供了大量的追加操作,类似String的"+",这个方法可以添加char类型 |
3 | public StringBuffer append(String str) | 方法 | 这个方法可以添加String类型 |
4 | public StringBuffer append(StringBuffer sb) | 方法 | 这个方法可以添加StringBuffer类型 |
5 | public int indexOf(String str) | 方法 | 查找指定字符串是否存在 |
6 | public int indexOf(String str,int fromIndex) | 方法 | 从指定位置开始查找指定字符串是否存在 |
7 | public StringBuffer insert(int offset,String str) | 方法 | 在指定位置处加上指定字符串 |
8 | public StringBuffer reverse() | 方法 | 将内容反转保存 |
9 | public StringBuffer replace(int start,int end ,String str) | 方法 | 指定内容替换 |
10 | public int length() | 方法 | 求出内容长度 |
11 | public StingBuffer delete(int start,int end) | 方法 | 删除指定范围内的字符串 |
12 | public String substring(int start) | 方法 | 指定开始点进行字符串截取 |
13 | public String substring(int start,int end) | 方法 | 指定范围进行字符串截取 |
14 | public String toString() | 方法 | 将内容变为String类型 |
2.1 使用append方法进行字符串连接
示例:
StringBuffer sb = new StringBuffer("hello");
sb.append("world").append("!!!");
System.out.println(sb.toString());
结果:
helloworld!!!
2.2 使用insert方法给StringBuffer对象添加内容
示例:
StringBuffer sb = new StringBuffer("world");
sb.insert(0,"hello");
System.out.println(sb.toString());
结果:
helloworld
2.3 反转字符串
示例:
StringBuffer sb = new StringBuffer("world");
sb.insert(0,"hello");
System.out.println(sb.toString());
System.out.println( sb.reverse().toString());//reverse方法返回一个反转后的 StringBuffer对象
结果:
helloworld
dlrowolleh
2.4 替换指定范围的内容
示例:
StringBuffer sb = new StringBuffer("world");
sb.insert(0,"hello");
System.out.println(sb.toString());
sb.replace(5,10,"robot);//将world变为robot
System.out.println(sb.toString());
结果:
helloworld
hellorobot
2.5 字符串截取
示例:
StringBuffer sb = new StringBuffer("world");
sb.insert(0,"hello");
System.out.println(sb.toString());
String str = sb.substring(5,10);//截取world
System.out.println(str);
结果:
helloworld
world
2.6 删除指定范围字符串
示例
StringBuffer sb = new StringBuffer("world");
sb.insert(0,"hello");
System.out.println(sb.toString());
System.out.println(sb.delete(5,10).toString());//删除world
结果:
helloworld
hello
2.7 查找指定内容是否存在
示例
StringBuffer sb = new StringBuffer("world");
sb.insert(0,"hello");
System.out.println(sb.toString());
if(sb.indexOf("hello")== -1) {//找不到便返回-1
System.out.println("该字符串不存在");
}else{
System.out.println("可以查找到指定的内容");
}
结果:
可以查找到指定的内容