索引
- Java常用类
-
- 1. String(字符串相关的类)
- 1.1String对象的创建
- 1.2.String不同拼接操作的对比
- 解决一个拼接问题
- 2. String常用方法
- 2.1 value.length 和charAt(int index)
- 2.2 isEmpty
- 2.3 toLowerCase()
- 2.3 toUpperCase()
- 2.4 trim()
- 2.5 equals和equalsIgnoreCase
- 2.6 cancat操作(连接)
- 2.7 compareTo(比较String的大小)
- 2.8 substring(截取字符串)
- 2.9 判断字符串(endWith startWith StarsWith)
- 3.0 contains(是否包含指定字符串)
- 3.1 indexOf(字符串索引)
- 3.2 String replace(替换)
- 3.3 matches(匹配)
- 3.4 split(切片)
- 3.5 类型转换
- 3.6 StringBuffer和StringBuilder
Java常用类
1. String(字符串相关的类)
String实例化的方式
通过字面量定义的方式
通过new+构造器的方式
- String类:代表字符串。Java程序中所有字符串字面值(”abc“)都作为此类的实现表现
- String是一个final类(不可被继承),代表不可变的字符序列
- 字符串是一个常量,用双引号引起来表示。他们的值在创建后就不能更改
- String对象的字符内容是存储在一个字符数组value[]中的
- 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中
- 字符串常量池中是不会存储相同内容的字符串的。
String s1 ="abc";
String s2 ="abc";
s1="hello";
//s1和s2地址值不一样,s1改为hello后s2还是abc,s1变成hello
1.1String对象的创建
String str1 = "abc";
String s1 = new String();
public void test2{
String s1 = "JavaEE";
String s2 = "JavaEE";
String s3 = new String("JavaEE");
String s4 = new String ("JavaEE");
System.out.println(s1==s2);//true
System.out.println(s1==s3);//flase
System.out.println(s1==s4);//flase
System.out.println(s3==s4);//flase
}
面试题
- String s = new String(“abc”);方式创建对象,在内存中创建了几个对象?
- 两个;一个是堆空间中的new结构,另一个是char [ ]对应的常量池中的数据“abc”。
1.2.String不同拼接操作的对比
public void test3(){
String s1 = "JavaEE";
String s2 = "hadoop";
String s3 = "JavaEEhadoop";
String s4 = "JavaEE"+"hadoop";
String s5 = s1+"hadoop";
String s6 = "JavaEE"+s2;
String s7 = s1+s2;
System.out.println(s3==s4);//true
System.out.println(s3==s5);//false
System.out.println(s3==s6);//false
System.out.println(s3==s7);//false
System.out.println(s5==s6);//false
System.out.println(s5==s7);//false
System.out.println(s6==s7);//false
String s8 = s5.intern();//返回值得到的s8在常量池中,而且已经存在"JavaEEhadoop",结果就返回s3
System.out.println(s3==s8);//true
}
说明
- 常量和常量的拼接结果在常量池,常量池中不会存在相同内容的常量。
- 只要其中有一个变量,结果就在堆中。
- 如果拼接的结果调用intern()方法,返回值就在常量值中。
解决一个拼接问题
- 常量和常量拼接的结果在常量池,且常量池中不会存在相同内容的常量
- 只要其中有一个是变量,结果就在堆中
- 如果拼接的结果调用intern()方法,返回值就在常量池中。
public void test(){
String s1 = "javaEEhadoop";
String s2 = "javaEE";
String s3 = s2+"hadoop";
System.out.println(s1==s3);
final String s4 = "javaEE";//添加final后变成
String s5 = s4+"hadopp"
System.out.println(s1==s5);
}
}
//输出结果
false
true
因为s3由一个变量和一个常量拼接,结果在堆中。而s1是一个常量,所以在常量池中
而s5是由常量和常量拼接的,所以和s1一样都在常量池中
2. String常用方法
- int length():返回字符串的长度:return value.length
- char charAt(int index):返回某索引出的字符 return value[index]
- boolean isEmpty:判断是否是空字符串:return value.length == 0
- String to LowerCase():使用默认语言环境,将String中的所有字符转化成小写
- String to toUpperCase():使用默认语言环境,将String中的所有字符转化成大写
- String trim():返回字符串的副本,忽略字符串的内容是否相同
- boolean equals(Object obj):比较字符串的内容是否相同
- boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写
- String concat(String str):将指定字符串连接到此字符串的结尾。等价于用“+”
- int compareTo(String anotherString):比较两个字符串的大小
- String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后一个字符串
- String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串
2.1 value.length 和charAt(int index)
public void test1(){
String s1 = "helloworld";
System.out.println(s1.length());
System.out.println(s1.charAt(0));
System.out.println(s1.charAt(9));
System.out.println(s1.charAt(10))
}
//输出结果
10
h
d
数组角标越界
2.2 isEmpty
public void test1(){
String s1 = "helloworld";
System.out.println(s1.length());
System.out.println(s1.charAt(0));
System.out.println(s1.charAt(10))
}
//输出结果
10
h
d
flase
2.3 toLowerCase()
将大写变成小写
public void test1(){
String s1 = "HelloWorld";
String s2 = s1.toLowerCase();
System.out.println(s1);
System.out.println(s2);
}
//输出结果
HelloWorld
helloworld
2.3 toUpperCase()
public void test1(){
String s1 = "HelloWorld";
String s2 = s1.toLowerCase();
System.out.println(s1);
System.out.println(s2);
}
//输出结果
HelloWorld
HELLOWORLD
2.4 trim()
去掉字符串两边的空格
public void test1(){
String s3 = " he llo world ";
String s4 = s3.trim();
System.out.println("-------"+s3+"----------");
System.out.println("-------"+s4+"----------");
}
//输出结果
------- he llo world ----------
-------he llo world----------
2.5 equals和equalsIgnoreCase
- 前者比较值是否相同,下列显然不相同(大小写)
- 后者忽略大小写
public void test(){
String s1 = "HelloWorld";
String s2 = "helloworld";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
}
2.6 cancat操作(连接)
public void test(){
String s1 = "abc";
String s4 = s3.concat("def");
System.out.println(s4);
}
//输出结果
abcdef
2.7 compareTo(比较String的大小)
public void test(){
String s1 ="abc";
String s2 = new String("abe");
System.out.println(s1.compareTo(s2));
//输出结果
-2
涉及到字符串排序
- s1.compareTo(s2)将执行的操作是,第一个字符串的第一个字符和第二个字符串的第一个字符比,s1-s2,因为前两个相同,而第三个为97-99(ASCII),所以结果为-2
2.8 substring(截取字符串)
- String substring(int beginIndex);
- 此字符串从beginIndexi开始截取,一直到末尾
- String substring(int beginIndex,int endIndex);
- 从beginIndex截取到endIndex
public void test(){
String s7="武汉生物工程学院";
String s8 =s7.substring(2);
System.out.println(s7);
System.out.println(s8);
}
//输出结果
武汉生物工程学院
生物工程学院
public void test(){
String s7="武汉生物工程学院";
String s9 =s7.substring(2,6);//从第2位截取到第6位
System.out.println(s7);
System.out.println(s9);
}
//输出结果
武汉生物工程学院
生物工程
2.9 判断字符串(endWith startWith StarsWith)
1 .endWith
判断此字符串是否以指定的后缀结束
public void test(){
String str1 = "helloworld";
boolean b1 = str1.endsWith("ld");