String详解
String概述
- 字符串是常量,创建之后不能改变。
- 字符串字面值储存在字符串池中,可以共享。
String s = "Hello"
产生一个对象,字符串池中存储。String s = new String("Hello");
产生两个对象,池、堆中各存储一个。
package com.lin.clas.demo06;
public class Test {
public static void main(String[] args) {
String name1 = "张三";
name1 = "李四";//给字符串赋值时,没有修改数据,而是重新开辟一个空间存储新的数据
String name2="李四";
//栈:name1[0x1122]->方法区(张三)---》name1[0x1122]->方法区(李四)---->name2[0x1122]->方法区(李四)
//堆:
//方法区:字符串池(张三)->字符串池((张三),(李四))
//另一种创建方式
String str = new String("abc");
//栈:str[0x1234]->堆(abc)
//堆:创建一个对象(abc)(指向字符串池中”abc“)
//方法区:创建一个空间(abc)
String str1 = new String("java");
String str2 = new String("java");
System.out.println(str1 == str2);//false
//指向堆中的对象不一样,“==”比较的是地址值,所以结果为false
System.out.println(str1.equals(str2));//true
}
}
String中常用方法
- public int length();返回字符串长度
- public char charAt(int index); 根据索引获取字符。
- public boolean contains(String str);判断当前字符串中是否包含str。
- public char[] toCharArray();将字符串转化为数组。
- public int indexOf(String str);//查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1.
- public int lastIndexOf(String str);查找字符串在当前字符串中最后一次出现的下标索引。
- public String trim();去掉字符串前后的空格
- public String toUpperCase();将小写转化成大写
- public boolean endWith(String str);判断字符串是否以str结尾
- public String replace(char oldChar,char newChar);将旧字符串替换成新字符串。
- public String[] split(String str);根据str做拆分。
package com.lin.clas.demo07;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
//1. length();返回字符串的长度
//2. charAt(int index);返回某索引处的字符
//3. contains(String str);判断是否包含某字符串
String content = "我爱你中国";
System.out.println(content.length());//5
System.out.println(content.charAt(0));//我
System.out.println(content.charAt(1));//爱
System.out.println(content.charAt(2));//你
System.out.println(content.charAt(3));//中
System.out.println(content.charAt(4));//国
try {
System.out.println(content.charAt(5));//StringIndexOutOfBoundsException
} catch (Exception e) {
System.out.println("StringIndexOutOfBoundsException");
}
System.out.println(content.contains("爱"));//true
System.out.println(content.contains("你爱"));//false
System.out.println("-------------");
//4.toCharArray();将字符串转化为字符数组
//5.indexOf(String str);返回某字符串在字符串中第一次出现的索引
//6.lastIndexOf(String str);返回某字符串在字符串中最后一次出现的索引
String content2 = "我爱你中国";
System.out.println(Arrays.toString(content2.toCharArray()));
System.out.println(content2.indexOf("爱"));//1
System.out.println(content2.lastIndexOf("中"));//3
System.out.println("-------------");
//7.trim();去除字符串两端的空格
//8.toUpperCase();将字符串转化为大写
//9.toLowerCase();将字符串转化为小写
//10.endWith(String str);判断是否以某字符串结尾
//11.startsWith(String str);判断是否以某字符串开头
String content3 = " I love China ";
System.out.println(content3.trim());
System.out.println(content3.toUpperCase());
System.out.println(content3.toLowerCase());
content3 = "hello world";
System.out.println(content3.endsWith("world"));//true
System.out.println(content3.startsWith("hello"));//true
System.out.println("-------------");
//12.replace(String oldStr,String newStr);将字符串中的某字符串替换为新的字符串
//13.split(String str);将字符串按照某字符串分割为字符串数组,返回值为字符串数组
String content4 = "I love China";
System.out.println(content4.replace("China", "World"));
System.out.println(Arrays.toString(content4.split(" ")));
System.out.println(Arrays.toString(content4.split("o")));
System.out.println("-------------");
//14.euqals(String str);判断两个字符串是否相等
//15.compare();比较两个字符串的大小
String s1 = "abc";
String s2 = "ABC";
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true//忽略大小写
}
}
String案例演示
需求:
- 已知String str = “this is a text”;
- 将str中的单词单独获取出来
- 将str中的text替换成practice
- 在text前面插入一个easy
- 将每个单词的首字母改为大写
package com.lin.clas.demo08;
/*
需求:
1. 已知String str = "this is a text";
2. 将str中的单词单独获取出来
3. 将str中的text替换成practice
4. 在text前面插入一个easy
5. 将每个单词的首字母改为大写
*/
import com.sun.scenario.effect.impl.sw.java.JSWBlend_SRC_OUTPeer;
public class Test {
public static void main(String[] args) {
String str = "this is a text";
//将str中的单词单独获取出来
String[] strArray = str.split(" " );
for(String x: strArray){
System.out.println(x);
}
System.out.println("------------");
//将str中的text替换成practice
String str1 = str.replace("text", "practice");
System.out.println(str1);
System.out.println("------------");
//在text前面插入一个easy
String str2 = str.replace("text", "easy text");
System.out.println(str2);
System.out.println("------------");
//将每个单词的首字母改为大写
String[] strArray1 = str.split(" " );
for(String x: strArray1){
//substing()方法返回一个新字符串,它是此字符串的一个子字符串。
String str3 = x.substring(0,1).toUpperCase() + x.substring(1);
System.out.println(str3);
}
}
}
StringBuffer和StringBuilder
- StringBuffer:可变长字符串,JDK1.0提供,运行效率慢,线程安全。(快慢相对于StringBuilder来说的,比String快)
- StringBuilder:可变长字符串,JDK5.0提供,运行效率快,线程不安全。
package com.lin.clas.demo09;
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
//1.append()方法:添加数据,并返回对象本身
sb.append("java");
System.out.println(sb.toString());
sb.append(" is").append(" very").append(" good");
System.out.println(sb.toString());
//java is very good
//2.insert()方法:在指定位置插入数据,并返回对象本身
sb.insert(4, "程序设计语言");
System.out.println(sb.toString());
//java程序设计语言 is very good
//3.replace()方法:替换指定位置的数据,并返回对象本身
sb.replace(4, 10, "编程语言");
System.out.println(sb.toString());
//java编程语言 is very good
//4.delete()方法:删除指定位置的数据,并返回对象本身
sb.delete(4, 8);
System.out.println(sb.toString());
//java is very good
//5.reverse()方法:反转数据,并返回对象本身
//6.delete()方法:删除指定位置的数据,并返回对象本身
sb.delete(0, sb.length());
System.out.println(sb.toString());
}
}