String类
-
String的两种实例化方式
直接赋值(用的最多)
String str=“Hello”;
传统方法,实例化对象
String str=new String("Hello");
-
字符串相等比较
str.equals(str1); //比较的是内容
public boolean equals(String str1)
-
字符串常量是String类的匿名对象
tips:在指定内容比较时,将指定内容(字符串常量)写在前面,避免NullPointException
面试题:请解释String类“==”与“equals”的区别
-
“==”:进行的是数值比较,比较的是两个字符串对象的内存地址数值
-
“equals()”:可以进行字符串内容的比较
-
String采用共享设计模式 在JVM底层自动维护一个字符串对象池(对象数组)。
如果采用直接赋值的模式进行String类的对象实例化操作,此对象将自动保存到对象池中,如果有,直接引用,如果没有,开辟新的空间将其保存到对象池中共下次使用;
对象池就是一个对象数组目的就是减少内存的开销
字符串手工入池操作:
public native String intern();
范例:
-
// 该字符串常量并没有保存在对象池之中
-
String str1 =
new
String(
"hello");
-
String str2 =
"hello" ;
-
System.out.println(str1 == str2);
// false
-
-
----------------------------------
-
----------------------------------
-
-
// 使用手动入池操作
-
String str1 =
new
String(
"hello").intern() ;
-
String str2 =
"hello" ;
-
System.out.println(str1 == str2);
// true
面试题:请解释String类中两种对象实例化的区别
-
直接赋值 就会自动采用共享模式,只会开辟一块堆内存空间,并且该字符串对象可以自动保存在对象池中以供下次使用。
-
构造方法 就会开辟两块堆内存空间并且其中一块堆内存将成为垃圾空间,不会自动保存在对象池中,可以使用intern()方法手工入池。
-
字符串常量不可变更
字符串常量一旦定义不可改变
范例:观察如下代码
-
String str =
"hello" ;
-
str = str +
" world" ;
-
str +=
"!!!" ;
-
System.out.println(str);
// hello world!!!
以上字符串变更是字符串对象变更而非字符串常量
内存中的字符串修改如下图所示:
可以发现字符串上没有发生变化,但是字符串对象的引用一直在改变,并且形成了大量垃圾空间。正是因为 String的特点,所以如下代码不应该在你的开发中出现:
-
String str =
"hello" ;
-
for(
int x =
0 ; x<
1000 ; x++) {
-
str += x ;
-
}
-
System.out.println(str)
如果有很多用户都使用了同样的操作,那么产生的垃圾数量就相当可观了。
字符串使用原则
-
字符串使用就采用直接赋值
-
字符串比较就使用equals()实现
-
字符串不要改变太多,防止产生过多垃圾空间
-
字符与字符串 String <-> char[]
1. 字符数组 char[]-> String
功能:将字符数组中所有内容变为字符串
构造函数为:public String ( char[] value1 ) ;
功能:将部分字符数组中的内容变为字符串
构造函数为:public String ( char[] value1, int offset, int count ) ;
2. String -> char
功能:将字符串按照索引转为单个字符
普通函数:public char charAt ( int index ) ;
如果使用charAt()方法超出了字符串长度,则会产生StringIndexOutOfBoundsException异常
3. String -> char[] *** 重点 ***
功能:将字符串转为字符数组
普通函数:public char[] toCharArray( ) ;
范例:
-
public
class Test{
-
public static void main(String[] args){
-
String str =
"helloworld" ;
-
// 将字符串变为字符数组
-
char[] data = str.toCharArray() ;
-
for (
int i =
0; i < data.length; i++) {
-
data[i] -=
32 ;
-
System.out.print(data[i]+
"、");
-
}
-
// 字符数组转为字符串
-
System.out.println(
new String(data));
// 全部转换
-
System.out.println(
new String(data,
5,
5));
// 部分转换
-
}
-
}
-
//输出
-
//H、E、L、L、O、W、O、R、L、D、HELLOWORLD
-
//WORLD
面试题:判断给定字符串是否由数字组成? *** 重要 ***
-
public
class Test{
-
public static void main (String[] args){
-
String str=
"123";
-
boolean a=MyAdjust(str);
-
if(a==
true){
-
System.out.println(
"字符串是由数字组成!");
-
}
-
else{
-
System.out.println(
"字符串中含有非数字成员!");
-
}
-
}
-
public static boolean MyAdjust(String str){
-
//将字符串转换为字符串数组
-
char[] data=str.toCharArray();
-
for(
int i=
0;i<data.length;i++){
-
if(data[i]<
'0'||data[i]>
'9'){
-
return
false;
-
}
-
}
-
return
true;
-
}
-
}
-
字节与字符串
功能:字节数组转为字符串 byte[] -> String
String构造方法:public String(byte[] bytes)
功能:字符串转为字节数组 String -> byte[] *** 非常重要 ***
String普通方法:public byte[] getBytes(String charset);按照指定编码转为字节数组
乱码的产生(编解码不一致,顺序不一致)
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
String str =
"helloword";
-
byte[] data = str.getBytes();
-
for (
int i =
0; i < data.length; i++) {
-
data[i] -=
32;
-
System.out.print(data[i]+
" ,");
-
}
-
System.out.println(
new String(data));
-
}
-
}
-
//编译结果如下:
-
//72 ,69 ,76 ,76 ,79 ,87 ,79 ,82 ,68 ,HELLOWORD
-
字符串比较
不区分大小写的比较方法 ( 比如输入验证码 )
public boolean equalsIgnoreCase(String anotherString)
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
String str1=
"helloword";
-
String str2=
"HELLOWORD";
-
System.out.println(str1.equals(str2));
-
System.out.println(str1.equalsIgnoreCase(str2));
-
}
-
}
-
//编译结果如下:
-
//false
-
//true
-
比较两个字符串大小关系 *** 重要 ***
public int compareTo(String anotherString) ( 只需要比到第一个不同的字符即可 )
1. >0 : 表示本字符串大于目标字符串
2. =0 : 表示两者相等
3. <0 : 表示本字符串小于目标字符串
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
System.out.println(
"A".compareTo(
"a"));
-
System.out.println(
"a".compareTo(
"A"));
-
System.out.println(
"A".compareTo(
"A"));
-
System.out.println(
"AB".compareTo(
"AC"));
-
System.out.println(
"代".compareTo(
"李"));
-
}
-
}
-
//编译结果如下:
-
//-32
-
//32
-
//0
-
//1
-
//-6251
-
字符串查找
从一个完整的字符串当中判断一个字符串是否存在 *** 重要 ***
public Boolean contains(Sting str);
范例:
-
public
class
Test {
-
public static void main(String[] args) {
-
String str=
"Helloworld";
-
System.
out.println(str.contains(
"world"));
-
}
-
}
-
//编译结果如下
-
//true
判断是否以指定字符串开头
public boolean startWith(String prefix)
public boolean startWith(String prefix,int toffset)toffset(偏移量)
判断是否以指定字符串结尾
public boolean endsWith(String suffix)
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
String str=
"Helloworld";
-
System.out.println(str.startsWith(
"H"));
-
System.out.println(str.startsWith(
"l",
2));
-
System.out.println(str.startsWith(
"e"));
-
System.out.println(str.endsWith(
"d"));
-
}
-
}
-
//编译结果如下:
-
//true
-
//true
-
//false
-
//true
-
字符串替换
public String replaceAll(String regex,String replacement)
将目标字符串全部替换
str.replaceAll(“l”,“_”):将str中的所有 "l" 换成 "_"
public String replaceFirst(String regex,String replacement)(替换首个)
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
String str=
"helloworld";
-
System.out.println(str.replaceAll(
"l",
"*"));
-
System.out.println(str.replaceFirst(
"l",
"*"));
-
}
-
}
-
//编译结果如下:
-
//he**owor*d
-
//he*loworld
-
字符串拆分 *** 重要 ***
将字符串全部拆分
public String[] split(String regex)
将字符串拆分成数组长度为limit的字符串数组
public String[] split(String regex,int limit)
范例:
-
public
class Test{
-
public static void main(String[] args){
-
String str=
"hello world hello bit and you";
-
String[] result=str.split(
" ",
2);
-
//String[] result=str.split(" ",3);
-
//String[] result=str.split(" ",4);
-
//String[] result=str.split(" ",5);
-
for(String s:result){
-
System.out.print(s+
" , ");
-
}
-
}
-
}
-
//编译结果如下:
-
//hello ,world hello bit and you
-
//hello ,world, hello bit and you
-
//hello ,world ,hello ,bit and you
-
//hello ,world ,hello, bit, and you
特殊字符需要转义后拆分 \\
eg:\\.
范例: 实现多次拆分 *** 重要 ***
-
public
class Test{
-
public static void main(String[] args){
-
String str=
"yum:21|hsd:176";
-
String[] temp=str.split(
"\\|");
-
for(
int i=
0;i<temp.length;i++){
-
String name=temp[i].split(
":")[
0];
-
String age=temp[i].split(
":")[
1];
-
System.out.println(
"姓名为:"+name);
-
System.out.println(
"年龄为:"+age);
-
}
-
}
-
}
-
字符串截取 *** 重要 ***
从指定索引截取到结尾
public Srting substring (int beginIndex)
从指定索引截部分内容 左闭右开 [ )
public String substring(int beginIndex,int endIndex)
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
String str=
"helloworld";
-
System.out.println(str.substring(
5));
-
System.out.println(str.substring(
0,
5));
-
}
-
}
-
//world
-
//hello
-
字符串其他操作方法
去除字符串左右空格,并且保留中间空格
public String trim() 可以与replaceAll配合使用 “ hello world ”
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
String str=
" hello world ";
-
System.out.println(
"["+str+
"]");
-
System.out.println(
"["+str.trim()+
"]");
-
}
-
}
-
//[ hello world ]
-
//[hello world]
字符串转大小写(全部大小写)(两个函数只是在字母之间进行大小写转换)
public String toUpperCase() //转大写
public String toLowerCase() //转小写
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
String str=
" Ig 牛逼 ";
-
System.out.println(str.toUpperCase());
-
System.out.println(str.toLowerCase());
-
}
-
}
-
// IG 牛逼
-
// ig 牛逼
范例:只将首字母转为大写 先截取,再转 面试题: *** 重要 ***
-
public
class Test {
-
public static void main(String[] args) {
-
System.out.println(firstUpper(
"dyson"));
-
System.out.println(firstUpper(
""));
-
System.out.println(firstUpper(
"a"));
-
}
-
public static String firstUpper(String str) {
-
//首先要判断str是否为空指针再接着判断str是否为空字符串
-
//注意先后顺序
-
if(str==
null||str.isEmpty()){
-
return str;
-
}
-
//由于从指定索引截部分内容为左闭右开 [ ),所以当字符串只有
-
//一个字符时要特殊处理
-
if(str.length()==
1){
-
return str.toUpperCase();
-
}
-
//先将字符串中第一个字符截取出来并转化为大写,再将后面的字符
-
//连接到该字符后边
-
return str.substring(
0,
1).toUpperCase()+str.substring(
1);
-
}
-
}
-
//Dyson A
- 判断字符串是否为空字符串(“”,不判断null,空指针)
public boolean isEmpty()
if(str==null | | str.isEmpty()){
//首先要判断str是否为空指针再接着判断str是否为空字符串
//注意先后顺序
}
范例:
-
public
class Test {
-
public static void main(String[] args) {
-
System.out.println(
"hello".isEmpty());
-
System.out.println(
"".isEmpty());
-
System.out.println(
new String().isEmpty());
-
}
-
}
-
//false true true