能够表示字符串:String类,StringBuffer类,StringBuilder类。
- String类:字符串内容不可以改变。
- StringBuffer和StringBuilder:内容可以改变。
一、String类:内容不能更改
1.字符串常量
字符串的数据是常量,存储在字符串池中,字符串的数值是不能更改的。字符串池中不允许存储重复的字符串。因为复用性。
字符串:0-多个字符的序列。使用双引号引起来的内容。
"",字符串对象存在,但是里面没有字符
"a",字符串中有一个字符
"abc",
"hello world",
注意:'a'和"a"
char c = 'a';//基本数据类型
String s = "a";//引用类型,String类的
2.null和“”
null,是指字符串对象压根就不存在,内存中没有
"",空字符串:字符串对象存在的,开辟内存,但是内容是空的。
3.字符串下标
字符串中的每个字符,其实都有固定的下标,index,从0开始,到长度减1。超出范围就下标越界。同数组类似。
4.String类的常用方法
1、创建字符串:
//1.直接创建
String s1 = "abc";
//2.通过构造方法
String s2 = new String();//""
//3.其他的构造方法
new String("abc");
new String(byte[]);
new String(byte[] ,offset ,length);
new String(char[])
new String(char[] ,offset,count);
//....
2、常用方法:
A:搜索类
indexOf(int)-->int
indexOf(String)-->int
indexOf(int ,fromIndex)-->int
indexOf(String,fromIndex)-->int
lastIndexOf(int)
lastIndexOf(String)
lastIndexOf(int,fromIndex)
lastIndexOf(String,fromIndex)
B:判断类
contains()-->boolean,是否包含指定内容
equals()-->boolean,判断字符串的字面值,是否相等,重写Object类
equalsIgnoreCase()-->boolean,忽略大小写的比较
startsWith()-->boolean,是否以指定内容开头
endsWith()-->boolean,是否以指定内容结尾
C:获取新的字符串
concat()-->String,拼接,作用同+
subString(fromIndex)--->String,截取子串
subString(fromIndex,endIndex)-->String,包含fromIndex,不包含endIndex
replace(oldchar,newchar)-->String,替换
replace(CharSequence,CharSequence)-->String
trim()->String,忽略首尾空格
toLowerCase()-->String,转小写
toUpperCase()-->String,转大写